DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSJAVA FUNDAMENTALS

Arrays: Practice Problems I

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

A traversal produces an answer only when each result variable has one precise job

Use the array [-4, -2, -7, -1] with n = 4. A single for loop can inspect each index, but the loop does not decide what the answer means. You must give each result variable one job before the first iteration. A running sum adds every value, while an even-value counter increases only when the current value has remainder 0 after division by 2.

JAVAThe sum changes for every value, but the counter changes only for even values.
int[] a = {-4, -2, -7, -1};
int n = 4;
int sum = 0;
int evenCount = 0;

for (int i = 0; i < n; i++) {
    sum = sum + a[i];
    if (a[i] % 2 == 0) {
        evenCount = evenCount + 1;
    }
}
INDEXVALUESUMEVENCOUNT
0-4-41
1-2-62
2-7-132
3-1-142
State after each visited index

At index 0, adding -4 changes sum to -4, and -4 is even, so evenCount becomes 1. At index 1, sum becomes -6 and the counter becomes 2 because -2 is also even. At index 2, sum becomes -13, but the counter stays 2 because -7 is odd. At index 3, sum becomes -14 and the counter stays 2 because -1 is odd.

CHECKPOINT 1Not answered

What are the values of sum and evenCount after indices 0-2 of [-4, -2, -7, -1] have been processed?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Zero is a valid starting value for totals and counts, but not for every result

sum = 0 is correct before you have added any values because zero is the additive identity. Adding a value to zero gives that value without changing it. evenCount = 0 is also correct because no even values have been counted before the loop starts. These variables represent a total and a number of matches, so zero describes their empty starting state.

JAVAThe total and count have valid zero starts, but max does not.
int sum = 0;
int evenCount = 0;
int max = 0;

for (int i = 0; i < 4; i++) {
    sum = sum + a[i];
    if (a[i] % 2 == 0) {
        evenCount = evenCount + 1;
    }
    if (a[i] > max) {
        max = a[i];
    }
}

max = 0 is not a valid starting state for this array. Every value in [-4, -2, -7, -1] is less than 0, so the comparison a[i] > max never succeeds. The loop therefore produces max = 0, even though 0 is not present in the array. The sum still becomes -14 and the even-value count still becomes 2 because their starting values have meanings that do not depend on the array containing zero.

The first element gives the maximum a valid starting value

For a maximum, the starting value should already be a value that the loop has seen. In this array, set max = a[0], which is -4, then start the comparison loop at index 1. The first element has already supplied the initial answer, so the loop only needs to test the remaining values.

JAVAThe loop compares the unseen values with the first value as the initial maximum.
int max = a[0];

for (int i = 1; i < 4; i++) {
    if (a[i] > max) {
        max = a[i];
    }
}
INDEX COMPAREDVALUEMAX BEFOREMAX AFTER
1-2-4-2
2-7-2-2
3-1-2-1
Maximum after each comparison

The initial maximum is -4 from index 0. At index 1, -2 is greater, so max becomes -2. At index 2, -7 is not greater, so max stays -2. At index 3, -1 is greater, so max becomes -1. At every point, max is one of the values already seen, which makes it impossible for the answer to be an invented value such as 0.

CHECKPOINT 2Not answered

Replace max = 0 and the matching loop start so the maximum is found correctly for [-4, -2, -7, -1].

int max = 0;
for (int i = 0; i < 4; i++) {
    if (a[i] > max) {
        max = a[i];
    }
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

One traversal can maintain several independent answers

The three calculations can share one index-based loop because each one has its own update rule. sum changes for every value, evenCount changes only when the value is even, and max changes only when the value is greater than the current maximum. The updates are independent, so changing one result does not replace or reset another.

JAVAOne traversal keeps three answers, each with its own correct starting value and update.
int[] a = {-4, -2, -7, -1};
int sum = 0;
int evenCount = 0;
int max = a[0];

for (int i = 0; i < 4; i++) {
    sum = sum + a[i];

    if (a[i] % 2 == 0) {
        evenCount = evenCount + 1;
    }

    if (a[i] > max) {
        max = a[i];
    }
}
RESULT VARIABLEMEANINGFINAL VALUE
sumtotal of all four values-14
evenCountnumber of even values2
maxgreatest value in the array-1
Final answers from the combined traversal

The initial states are sum = 0, evenCount = 0, and max = a[0]. After all four indices are processed, sum is -14 because -4 + -2 + -7 + -1 equals -14. evenCount is 2 because -4 and -2 are even. max is -1 because it is greater than -4, -2, and -7. Each final answer follows from the meaning of its own variable.

Previous · Iterating Through ArraysNext part · Quiz