Arrays and Hashing Array basics

52. Check Whether an Array Is Sorted

Easy

You are given an array of integers nums. Return true if its elements are in non-decreasing order, and false otherwise.

Non-decreasing order means that every element is greater than or equal to the element just before it. Equal neighbours are allowed, so [2, 2, 3] is in order. An array with a single element is always in order.

Example 1

Input: nums = [-4, -1, 0, 0, 7]

Output: true

Explanation: Every element is at least as large as the one before it. The two zeros are equal neighbours, and equal neighbours are allowed.

Example 2

Input: nums = [2, 6, 9, 4, 11]

Output: false

Explanation: The 4 comes right after the 9 and is smaller than it. The array starts small and ends large, but that one step down is enough to break the order.

Example 3

Input: nums = [1000000000, -1000000000]

Output: false

Explanation: The largest value allowed comes first and the smallest comes second, so the order goes down.

Check your understanding

Work out the answer for this input before you write any code.

nums = [3, 3, 3]

Constraints

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

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.