Check if an Array is Sorted
Easy
The Check if an Array is Sorted problem involves determining whether the elements of an array are arranged in non-decreasing order.
Given an integer array arr[] of size n, return true if the array is sorted in non-decreasing order; otherwise, return false.
Note: An array is considered sorted if every element is less than or equal to its next element.
Example 1
Input
arr[] = [1,2,3,4,5]
Output
true
Explanation
Every element is less than or equal to the next element, so the array is sorted.
Example 2
Input
arr[] = [2,5,1,8,9]
Output
false
Explanation
Since 5 is greater than 1, the array is not sorted.
Constraints
1 <= n <= 10^5
-10^9 <= arr[i] <= 10^9
Hints:
Hint 1
Traverse the array once and compare every element with its next element. If any element is greater than the next one, the array is not sorted.
Auto
Loading editor...
Input
Expected Output