DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

An Array Is Sorted Only When Every Adjacent Pair Agrees

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

Endpoints cannot prove that an array is sorted

An array is in forward order when each value is less than or equal to the value immediately after it. It is in backward order when each value is greater than or equal to the value immediately after it. For [2, 4, 4, 1], the adjacent pairs are 2, 4, then 4, 4, then 4, 1. Each pair must obey one direction. Comparing only the endpoints, 2 and 1, skips the evidence between them.

TEXTThe direction is checked between neighbors, not only between the endpoints.
indices:  0  1  2  3
values:   2  4  4  1
pairs:      2,4  4,4  4,1

The first pair, 2 and 4, supports forward order because 2 <= 4. It does not support backward order because 2 >= 4 is false. The last pair, 4 and 1, supports backward order, but it breaks forward order. The array therefore cannot be sorted in either direction, even though its first value is greater than its last value.

CHECKPOINT 1Not answered

Which adjacent evidence prevents the endpoint comparison 2 versus 1 from proving backward order?

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

Equal neighbors do not break either direction

The middle pair in [2, 4, 4, 1] is 4, 4. It satisfies 4 <= 4, so it is allowed in forward order. It also satisfies 4 >= 4, so it is allowed in backward order. Equal neighboring values do not choose a direction and do not eliminate either direction.

Sorted order allows equal values. Strictly increasing order would require every next value to be larger, and strictly decreasing order would require every next value to be smaller. This check does not require strict changes, so the repeated 4 does not make the array unsorted by itself.

Two boolean flags preserve every comparison made so far

Start both the forward flag and the backward flag as true. A flag means that every adjacent pair checked so far still supports that direction. At index 1, compare the current value 4 with the preceding value 2. Forward remains true because 2 <= 4, while backward becomes false because 2 >= 4 is false.

CPPEach comparison can eliminate a direction, but it never restores one.
bool forward = true;
bool backward = true;

for (int i = 1; i < 4; i++) {
    if (arr[i - 1] > arr[i]) forward = false;
    if (arr[i - 1] < arr[i]) backward = false;
}

At index 2, compare 4 with 4. Neither flag changes because both 4 <= 4 and 4 >= 4 are true. At index 3, compare 1 with the preceding 4. Forward becomes false because 4 <= 1 is false, while backward stays false because it was already eliminated. Once a flag becomes false, a later pair cannot make the earlier contradiction disappear.

CHECKPOINT 2Not answered

Enter the final forward and backward flag values after tracing [2, 4, 4, 1].

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

the adjacent-pair trace of [2, 4, 4, 1] with forward and backward flagsThe array has index 0 value 2, index 1 value 4, index 2 value 4, and index 3 value 1. Both flags begin true. Comparing 2 with 4 keeps forward true and changes backward to false. Comparing 4 with 4 changes neither flag. Comparing 4 with 1 changes forward to false while backward stays false. The final false, false pair produces the classification not at all.24412, 4≤ true → F stays≥ false → B off4, 4≤ true, ≥ trueflags unchanged4, 1≤ false → F off≥ false → B stays offforwardtruetruetruefalsebackwardtruefalsefalsefalsenot at all0123initafter 2,4after 4,4after 4,1
A direction survives only while every adjacent pair supports it.

The final flag pair determines the classification

After every adjacent pair has been checked, use the flags in a fixed order. If forward is true, classify the array as forward. Otherwise, if backward is true, classify it as backward. If both are false, classify it as not at all.

The forward-first rule handles the case where every adjacent pair is equal, because both nondecreasing and nonincreasing are then true. Choosing forward first gives that case one consistent result. For [2, 4, 4, 1], the final pair is forward false and backward false, so the classification is not at all.

CPPCheck forward first, then backward, and use not at all only when both flags are false.
if (forward) {
    // classify as forward
} else if (backward) {
    // classify as backward
} else {
    // classify as not at all
}