5. Is the Ledger Ordered

EasyArray

A bookkeeper keeps a ledger of transaction amounts, one entry per line. The ledger counts as ordered when no entry is smaller than the entry just before it, so equal neighbours are fine.

Write a method isSorted that takes the array nums of ledger entries and returns true if the ledger is ordered from smallest to largest (non-decreasing), and false otherwise.

Example 1

Input:
nums = [1,2,2,5]
Output:
true
Explanation:

Each entry is at least as large as the previous one, so the ledger is ordered.

Example 2

Input:
nums = [4,7,3,9]
Output:
false
Explanation:

3 comes right after 7, which is larger, so the ledger is not ordered.

Constraints

1 ≤ nums.length ≤ 1000

-106 ≤ nums[i] ≤ 106

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…