DSA FUNDAMENTALS › ARRAY BASICS
For each value, test whether a[i] % 2 == 0. If the test is true, store the value in the even array. Otherwise, store it in the odd array. In [7, -3, 2, 0, 9, 4], the values 2, 0, and 4 pass the test, while 7, -3, and 9 take the other branch.
if (a[i] % 2 == 0) {
// store a[i] in the even output
} else {
// store a[i] in the odd output
}Using else for the odd case is safer than checking whether the remainder equals 1. In C++ and Java, -3 % 2 is -1, not 1, so a rule such as a[i] % 2 == 1 would miss this negative odd value. The even test still works because -3 does not produce remainder 0.
In the array [7, -3, 2, 0, 9, 4], which destinations receive -3 and 0?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The source index i tells you where the current value was found, not where it belongs in its compact output. In the running example, 7 is at source index 0 and -3 is at source index 1. They become odd[0] and odd[1], but the even value 2 appears between them in the input and must not create a gap in the odd array.
// Incorrect: i is a source position, not an odd-output position
odd[i] = a[i];If you write odd[i] = a[i] for every odd value, 7 goes to odd[0], -3 goes to odd[1], and 9 goes to odd[4]. The odd values are no longer a compact prefix, because source indices 2 and 3 belonged to even values. The same mistake can leave gaps in the even array when odd values appear before an even value.
Give both output arrays capacity n, because neither output can contain more than all n input values. Start oddWrite and evenWrite at 0. Each variable marks the next free position in its own output, so an odd write changes only oddWrite and an even write changes only evenWrite.
int oddWrite = 0;
int evenWrite = 0;
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
even[evenWrite] = a[i];
evenWrite++;
} else {
odd[oddWrite] = a[i];
oddWrite++;
}
}The trace begins with both write positions at 0. The value 7 is odd, so it fills odd[0] and oddWrite becomes 1. The value -3 fills odd[1] and oddWrite becomes 2. Then 2 fills even[0], changing evenWrite from 0 to 1 while oddWrite stays 2. Continuing this way preserves the input order inside both outputs.
Replace the incorrect destination index with the correct write position for this odd-value assignment.
odd[i] = a[i];Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After all six values are processed, oddWrite is 3 and evenWrite is 3. The odd result is the filled prefix odd[0..2] = [7, -3, 9], and the even result is the filled prefix even[0..2] = [2, 0, 4]. The arrays were allocated with capacity 6, but positions from index 3 onward are unused storage, not additional result elements.
The write positions therefore serve two purposes: they identify where the next value belongs and record the logical length of each result. To use the outputs, read odd from index 0 while the index is less than oddWrite, and read even from index 0 while the index is less than evenWrite. This keeps unused capacity separate from actual data.