00:00

Problem Statement

Input/Output Examples

Visual Explanation

There are only n - 1 neighbouring pairs to check, and one broken pair is enough to answer false. That is why the loop starts at index 1 and compares arr[i - 1] with arr[i].

Approaches

Both approaches compare neighbouring elements and stop early. One uses a loop; the other uses allMatch() on a stream of indexes.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of elements. In the worst case, when the array is sorted, all n - 1 pairs are compared; otherwise the check stops earlier.

Interview Tips and Recommended Approach

  • Clarify strict or non-strict order. Use <= when duplicates are allowed and < when the order must be strictly increasing.
  • Do not sort and compare. Copying, sorting and calling Arrays.equals() works but costs O(n log n) time and O(n) memory.
  • Watch the index range. A loop from 0 to n - 1 that reads arr[i + 1] must stop one element earlier.
  • Know the descending variant: flip the comparison, or check both directions in one pass.

Recommended Approach: Loop Over Neighbours

Why: it answers in a single pass with constant memory and stops at the first problem. Its index handling is simple to explain and easy to adapt to strict or descending order.