The idea

An array a is sorted in non-decreasing order if a[i-1] <= a[i] for every i >= 1. You only need to check adjacent pairs — if every pair is in order, the whole array is in order.

for i in 1..n-1:
    if a[i-1] > a[i]:
        return false
return true

That's it. O(n) time, O(1) memory.

Edge cases

  • Arrays of length 0 or 1 are trivially sorted.
  • Equal adjacent values are fine for non-decreasing order. [1, 2, 2, 3] is sorted.

Output

Print exactly true or false (lowercase) followed by a newline.

Exercise

Print 'true' if the array is sorted in non-decreasing order, otherwise 'false'.

main.cpp
Loading editor…
5 tests — click Submit to run all of them.
Visible cases
ascending
stdin
5
1 2 3 4 5
expected
true
with equal adjacent
stdin
4
1 2 2 3
expected
true
one out of order
stdin
5
1 3 2 4 5
expected
false