Monotonic Array

Updated on 18 June, 2025
Monotonic Array header image

Problem Statement

In the realm of computer programming, particularly in data analysis and processing, determining the nature of a sequence is a fundamental task. A sequence or an array is classified as monotonic if it consistently behaves in a single direction - either increasing or decreasing. More formally, an array nums is considered monotone increasing if for every pair of indices i and j where i <= j, it holds that nums[i] <= nums[j]. Conversely, an array is monotone decreasing if for every pair of indices i and j where i <= j, nums[i] >= nums[j] is true.

Given such an integer array nums, the objective is to determine whether the array is monotonic. The expected output is a boolean value: true if the array is either monotone increasing or decreasing, and false otherwise.

Examples

Example 1

Input:

nums = [1,2,2,3]

Output:

true

Example 2

Input:

nums = [6,5,4,4]

Output:

true

Example 3

Input:

nums = [1,3,2]

Output:

false

Constraints

  • 1 <= nums.length <= 105
  • -105 <= nums[i] <= 105

Approach and Intuition

To evaluate whether the given nums array is monotonic, we need a strategy that efficiently assesses its behavior across all elements. Here are some logical steps that outline our approach:

  1. Initial Thought:

    • Monotonicity implies uniform behavior either in an increasing order or a decreasing order throughout the array.
  2. Simple Observations:

    • If the array has a length of 1, it's trivially monotonic as there are no other elements to compare.
  3. Direction Assessment:

    • Start by examining the direction between the first two distinct elements. This sets a preliminary understanding of expected behavior (increasing or decreasing).
  4. Iterative Comparison:

    • Continue to check each subsequent element to ensure it adheres to the determined increasing or decreasing order. Any deviation from the set behavior immediately results in the array being non-monotonic, and we can return false.
  5. Final Assessment:

    • If the entire array adheres to the initial behavior set by its early members, return true.

Looking at the provided examples helps to solidify this understanding:

  • Example 1 (nums = [1,2,2,3]):

    • The array starts at 1 and increases or remains the same through to 3. This confirms our understanding of a monotone increasing array.
  • Example 2 (nums = [6,5,4,4]):

    • Here, the numbers start high and either decrease or remain stable as we progress. This confirms a monotone decreasing array.
  • Example 3 (nums = [1,3,2]):

    • The change from 1 to 3 suggests an increasing pattern, but the drop to 2 violates this presumption. Hence, the array isn’t monotonic.

The outlined approach will effectively determine the monotonicity of an array while respecting the bounds given in the constraints (1 <= nums.length <= 105 and -105 <= nums[i] <= 105). Each element is checked against the initial trend established, making the solution both intuitive and efficient.

Solutions

  • Java
java
class Solution {
    public boolean checkMonotonic(int[] nums) {
        boolean inc = true;
        boolean dec = true;
        for (int i = 0; i < nums.length - 1; ++i) {
            if (nums[i] > nums[i+1])
                inc = false;
            if (nums[i] < nums[i+1])
                dec = false;
        }

        return inc || dec;
    }
}

The task involves determining if an array is monotonic. An array is classified as monotonic if it is either continuously increasing or continuously decreasing. The provided solution is implemented in Java.

  • Define a class Solution with a method checkMonotonic which accepts an integer array nums.
  • Declare two boolean variables inc and dec, initialized to true. These denote if the array is increasing or decreasing respectively.
  • Use a for loop to traverse the array from the first element to the second-to-last element.
    • If any element is greater than the following element, set inc to false.
    • Similarly, if any element is less than the following element, set dec to false.
  • If either inc or dec remains true after checking all elements, the array is monotonic. The final result is returned by the || operator, which ensures the array qualifies as monotonic if either condition holds.

This implementation effectively captures whether an array maintains a single direction in terms of value changes, making it an efficient solution for checking the monotonic property.

Comments

No comments yet.