Recognize Nondecreasing or Nonincreasing Arrays

Quick Overview

Recognize Nondecreasing or Nonincreasing Arrays. Return whether nums is monotonic: either every earlier value is no greater than every later value, or every earlier value is no smaller than every later value.

Recognize Nondecreasing or Nonincreasing Arrays

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return whether `nums` is monotonic: either every earlier value is no greater than every later value, or every earlier value is no smaller than every later value. Equal adjacent values are allowed in either direction. ```cpp class Solution { public: bool isMonotonic(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text Input: nums = [4,4,2,-1] Output: true Explanation: Every step is nonincreasing, including the initial equality. ``` **Example 2:** ```text Input: nums = [2,2,5,3] Output: false Explanation: The rise2 to5 rules out nonincreasing order, while the fall5 to3 rules out nondecreasing order. ``` ### Constraints - `1 <= nums.length <= 10^5`. - `-10^5 <= nums[i] <= 10^5`. ### Contract considerations This is weak monotonicity; equal values do not break either direction. ```hint Read the boundary condition Which adjacent comparisons disprove each of the two possible directions? ``` ### Boundary reminders This is weak monotonicity; equal values do not break either direction.

Overview: Recognize Nondecreasing or Nonincreasing Arrays. Return whether nums is monotonic: either every earlier value is no greater than every later value, or every earlier value is no smaller than every later value.

Google logo
Google
Jun 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether nums is monotonic: either every earlier value is no greater than every later value, or every earlier value is no smaller than every later value. Equal adjacent values are allowed in either direction.

class Solution {
public:
    bool isMonotonic(vector<int>& nums);
};

Examples

Example 1:

Input: nums = [4,4,2,-1]
Output: true
Explanation: Every step is nonincreasing, including the initial equality.

Example 2:

Input: nums = [2,2,5,3]
Output: false
Explanation: The rise2 to5 rules out nonincreasing order, while the fall5 to3 rules out nondecreasing order.

Constraints

  • 1 <= nums.length <= 10^5 .
  • -10^5 <= nums[i] <= 10^5 .

Contract considerations

This is weak monotonicity; equal values do not break either direction.

Boundary reminders

This is weak monotonicity; equal values do not break either direction.