Easy
Check Whether an Array Is Sorted
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Arrays and Strings
Description
Given an array of integers, determine whether it is sorted in non-decreasing order.
An array is sorted in non-decreasing order when every element is less than or equal to the element immediately after it. Equal adjacent values are allowed.
Return true if the array is sorted; otherwise, return false.
For example, [1, 2, 2, 4] is sorted, while [1, 3, 2] is not.
Implement isSorted(nums) to return a boolean.
Constraints
1 <= nums.length <= 1000 -1000000 <= nums[i] <= 1000000
Sample cases
3Sample 1
Input
nums = [1,2,2,4]
Expected output
true
Sample 2
Input
nums = [4,3,2,1]
Expected output
false
Skills and languages
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer