DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

Create Compact Odd and Even Arrays with Independent Write Positions

Reading · 4 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

Testing for evenness gives every value exactly one destination

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.

CPPThe decision sends each input value to exactly one output.
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.

CHECKPOINT 1Not answered

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 does not identify the next free output slot

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.

CPPUsing i as the destination index leaves gaps when values are sent to the other output.
// 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.

Two output arrays require two independent write positions

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.

CPPEach branch writes at its own next free position and advances only that position.
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.

the array [7, -3, 2, 0, 9, 4] being distributed into odd and even output arraysThe input row has indexed cells 0:7, 1:-3, 2:2, 3:0, 4:9, and 5:4. A source index i visits the cells from left to right. Arrows route 7, -3, and 9 to odd indices 0, 1, and 2, while arrows route 2, 0, and 4 to even indices 0, 1, and 2. oddWrite advances only on odd values and ends at 3. evenWrite advances only on even values and ends at 3.7-32094parity decisionvalue % 2 == 0?7-39204oddWrite: 0 → 1 → 2 → 3evenWrite: 0 → 1 → 2 → 3value % 2 != 0value % 2 == 0source index i scans 0 → 5012345odd output012even output012
Each destination advances its own next free index.
CHECKPOINT 2Not answered

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.

Only the filled prefixes belong to the results

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.

Previous · Create a Duplicate of an ArrayNext part · Quiz