Menu
CoddyTech

Check if an Array Is Sorted

You get an array of integers nums. Return true if it is in non-decreasing order, meaning every element is less than or equal to the one after it, and false otherwise. Equal neighbours are fine: [2, 2, 3] counts as sorted. An array with one element is sorted.

Function

isSorted(nums: integer-array) → boolean
numsinteger-array
the array of integers to check
Returnsboolean
true when every element is at most the next one, false otherwise

Constraints

  • 1 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109

Examples

Input
nums = [1, 3, 3, 7]
Output
true
Explanation
Each step goes up or stays level: 1 to 3, 3 to 3, 3 to 7. The repeated 3 is allowed, so the answer is true.

lock icon+16 hidden tests on Submit

challenge icon

Follow-up

How would you check an array that may be sorted in either direction, increasing or decreasing, still in one pass?

Reset code
def isSorted(nums):
    # Write code here
Test cases

Case 1

Case 2

Input

nums = [1, 3, 3, 7]

Expected

true