Check If Array Is Sorted
Given an array of integers, determine if the array is sorted in non-decreasing order. Return true if it is sorted, otherwise return false.
[ [ 1, 2, 3, 4, 5 ] ]
Explanation. The array is sorted in non-decreasing order.
[ [ 5, 3, 2, 4, 1 ] ]
Explanation. The array is not sorted as 3 (position 2) is less than 5 (position 1).
[ [ -1, 0, 10, 10, 20 ] ]
Explanation. The array is sorted and even contains duplicate elements which are considered to be non-decreasing.
[ [ 100 ] ]
Explanation. A single element array is always considered sorted.
[ [ 100, -100 ] ]
Explanation. The array elements decrease and hence it is not sorted.
Follow-up: Can you solve this problem in O(n) time complexity?
1. The array will contain at least 1 and at most 1000 elements. 2. Each element in the array will be between -1000 and 1000.
- Views
- 5