4. Check if Array is Sorted

EasyArrayArray

Given an integer array nums, return true if the array is sorted in non-decreasing order, and false otherwise.

Non-decreasing means each element is less than or equal to the next element.

Input: An integer array nums of length n.

Output: true if the array is non-decreasing, false otherwise.

Examples

Example 1
Input: [1,2,3,4,5]
Output: true
Explanation: 1≤2, 2≤3, 3≤4, 4≤5 — all pairs satisfy non-decreasing order. Return true.
Example 2
Input: [5,4,3,2,1]
Output: false
Explanation: 5 > 4 at positions 0 and 1 — immediately violates the condition. Return false.
Example 3
Input: [1,1,2,2,3]
Output: true
Explanation: 1≤1, 1≤2, 2≤2, 2≤3 — equal elements are allowed. Return true.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →