Quick Overview

Decide whether an integer array is monotonic, meaning entirely non-decreasing or entirely non-increasing, with equal neighbors allowed. It tests a single linear pass over adjacent pairs, the reasoning for why neighbor checks are enough, and handling of repeated values.

Check whether an integer array is monotonic (entirely non-decreasing or non-increasing)

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums`, return `True` if the array is monotonic (either monotone increasing or monotone decreasing), and `False` otherwise. An array is monotone increasing if `nums[i] <= nums[j]` for all `i <= j`. An array is monotone decreasing if `nums[i] >= nums[j]` for all `i <= j`. ### Function Signature ```python def is_monotonic(nums: list[int]) -> bool: ``` ### Rules - Equal neighboring values are allowed in both directions, so a constant array is monotonic. - An array with one element is monotonic. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^5 <= nums[i] <= 10^5` ### Examples ```text Input: nums = [1, 2, 2, 3] Output: True ``` Each element is greater than or equal to the one before it, so the array is monotone increasing. The repeated 2 does not break that. ```text Input: nums = [6, 5, 4, 4] Output: True ``` Each element is less than or equal to the one before it, so the array is monotone decreasing. ```text Input: nums = [1, 3, 2] Output: False ``` The step from 1 to 3 goes up, so the array is not monotone decreasing, and the step from 3 to 2 goes down, so it is not monotone increasing either.

Overview: Decide whether an integer array is monotonic, meaning entirely non-decreasing or entirely non-increasing, with equal neighbors allowed. It tests a single linear pass over adjacent pairs, the reasoning for why neighbor checks are enough, and handling of repeated values.

Given an array of integers `nums`, return `True` if the array is monotonic and `False` otherwise. An array is monotonic when it is monotone increasing or monotone decreasing. The array is monotone increasing if `nums[i] <= nums[j]` for all indices `i <= j`. It is monotone decreasing if `nums[i] >= nums[j]` for all indices `i <= j`. Equal neighboring values are allowed in both directions, so a constant array is monotonic. An array with one element is also monotonic. Implement `is_monotonic(nums)`. Python returns `True` or `False`; JavaScript, Java and C++ return their boolean `true` or `false`. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^5 <= nums[i] <= 10^5` - No value can exceed 2^31-1, so every value fits in a signed 32-bit integer (`int[]` in Java, `std::vector<int>` in C++). ### Example 1 ```text Input: nums = [1, 2, 2, 3] Output: True ``` Each element is greater than or equal to the one before it, so the array is monotone increasing. The repeated 2 does not break that. ### Example 2 ```text Input: nums = [1, 3, 2] Output: False ``` The step from 1 to 3 goes up, so the array is not monotone decreasing, and the step from 3 to 2 goes down, so it is not monotone increasing either.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^5 <= nums[i] <= 10^5
  • No value can exceed 2^31-1, so every value fits in a signed 32-bit integer.

Examples

Input: ([1, 2, 2, 3],)

Expected Output: True

Explanation: Source example 1: every step rises or stays equal, and the repeated 2 is allowed.

Input: ([6, 5, 4, 4],)

Expected Output: True

Explanation: Source example 2: every step drops or stays equal, ending on a repeated 4.

Hints

  1. The definition compares every pair with i <= j. Ask whether looking at fewer pairs can decide the same question.
  2. Equal neighbors fit both directions, so until a strict rise or drop appears, the array could still be either kind.
  3. Try your idea on an array that starts with repeated values, and on one that changes direction only at its last step.

Check Whether an Array Is Monotonic

easyMetaSoftware Engineer

Given an array of integers nums, return True if the array is monotonic and False otherwise. An array is monotonic when it is monotone increasing or monotone decreasing.

The array is monotone increasing if nums[i] <= nums[j] for all indices i <= j. It is monotone decreasing if nums[i] >= nums[j] for all indices i <= j.

Equal neighboring values are allowed in both directions, so a constant array is monotonic. An array with one element is also monotonic.

Implement is_monotonic(nums). Python returns True or False; JavaScript, Java and C++ return their boolean true or false.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^5 <= nums[i] <= 10^5
  • No value can exceed 2^31-1, so every value fits in a signed 32-bit integer (int[] in Java, std::vector<int> in C++).

Example 1

Input: nums = [1, 2, 2, 3]
Output: True

Each element is greater than or equal to the one before it, so the array is monotone increasing. The repeated 2 does not break that.

Example 2

Input: nums = [1, 3, 2]
Output: False

The step from 1 to 3 goes up, so the array is not monotone decreasing, and the step from 3 to 2 goes down, so it is not monotone increasing either.

Examples

Example 1
Input
([1, 2, 2, 3],)
Output
True
Notes
Source example 1: every step rises or stays equal, and the repeated 2 is allowed.
Example 2
Input
([6, 5, 4, 4],)
Output
True
Notes
Source example 2: every step drops or stays equal, ending on a repeated 4.

Constraints

  • •1 <= len(nums) <= 10^5
  • •-10^5 <= nums[i] <= 10^5
  • •No value can exceed 2^31-1, so every value fits in a signed 32-bit integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def is_monotonic(nums: list[int]) -> bool:
Sample call
is_monotonic([1, 2, 2, 3])
Language
Case 1
Input
([1, 2, 2, 3],)
Expected
True
Case 2
Input
([6, 5, 4, 4],)
Expected
True

Browse More Questions

Show the approach

Approach

Scan the adjacent pairs once while keeping two flags, increasing and decreasing, that both start as true. A pair that drops (nums[i] < nums[i - 1]) rules out monotone increasing, so it clears increasing. A pair that rises (nums[i] > nums[i - 1]) rules out monotone decreasing, so it clears decreasing. An equal pair changes neither flag. The answer is increasing or decreasing.

After the pair ending at index i is processed, increasing is true exactly when nums[0..i] is non-decreasing, and decreasing is true exactly when it is non-increasing. The definition compares every pair i <= j, and because <= and >= are transitive, the condition holds for all pairs exactly when it holds for every adjacent pair. So after the full scan the two flags answer the two definitions, and the array is monotonic when at least one flag is still true.

A single element has no adjacent pairs, so both flags stay true and the result is True. A constant array has only equal pairs, so both flags also stay true. A leading plateau does not commit the scan to a direction, which is why deciding the direction from the first pair alone gives wrong answers on inputs such as [4, 4, 4, 2, 1, 1, 0]. All values fit in 32-bit integers and no arithmetic is done on them, so overflow cannot occur.

Time complexity:
O(n)
Space complexity:
O(1)