AlgoInsight

DSA problem

Check if an Array is Sorted

EasyArraysPattern: Array TraversalbeginnerFree

Given an array of N integers, check whether it is sorted in non-decreasing order.

Simply compare each element with the next one. If you find any pair where arr[i] > arr[i+1], the array is not sorted - return false immediately. If you make it through the entire array without finding such a pair, it is sorted.

This is a linear scan with early termination - the moment you find a violation, you can stop. The non-decreasing condition allows equal adjacent elements, so [1, 2, 2, 3] is sorted, but [1, 3, 2, 4] is not.

Examples

Example 1
Input: nums = [1, 2, 3, 4, 5]
Output: true
Explanation: Every element is less than or equal to the one after it, so the array is sorted.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

array

Browse all DSA problems