Monotonic Array — Solution in Python, Java & C++
Monotonic Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and Meta.
Monotonic Array is an easy coding problem on arrays, asked in Amazon, Meta and Capital One interviews.
- Difficulty: Easy
- Topics: Arrays
- Asked at: Amazon, Meta, Capital One
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
An array is monotonic if it is entirely non-increasing or entirely non-decreasing.
Given an integer array nums, return true if it is monotonic and false otherwise.
Example 1
Input: nums = [1,2,2,3]
Output: true
Explanation: Non-decreasing throughout.
Example 2
Input: nums = [6,5,4,4]
Output: true
Explanation: Non-increasing throughout.
Example 3
Input: nums = [1,3,2]
Output: false
Constraints
1 <= nums.length <= 100000-100000 <= nums[i] <= 100000
Hints for Monotonic Array
Hint 1
You do not need two passes — track both possibilities at once.
Hint 2
Keep two booleans,
increasinganddecreasing, both startingtrue.Hint 3
A strict drop kills
increasing; a strict rise killsdecreasing. Equal neighbours kill neither.
How to solve Monotonic Array
Monotonic means "non-decreasing OR non-increasing". Test both hypotheses in a single pass and see whether either survives.
Approach
- Start
inc = trueanddec = true. - For each adjacent pair, if
nums[i] < nums[i-1]setinc = false; ifnums[i] > nums[i-1]setdec = false. - Return
inc || dec.
Why it works
inc stays true exactly when no strict descent exists, which is the definition of non-decreasing; dec mirrors it. Equal neighbours satisfy both definitions, so they must clear neither flag.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Using
<=/>=in the clearing conditions makes a flat array like[2,2,2]reportfalse. - Deciding the direction from the first differing pair alone needs care when the array starts with a run of equal values.
Reference solution
Python
from typing import List
def isMonotonic(nums: List[int]) -> bool:
inc = True
dec = True
for i in range(1, len(nums)):
if nums[i] < nums[i - 1]:
inc = False
if nums[i] > nums[i - 1]:
dec = False
return inc or decJava
public static boolean isMonotonic(int[] nums) {
boolean inc = true, dec = true;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) inc = false;
if (nums[i] > nums[i - 1]) dec = false;
}
return inc || dec;
}C++
bool isMonotonic(vector<int>& nums) {
bool inc = true, dec = true;
for (int i = 1; i < (int) nums.size(); i++) {
if (nums[i] < nums[i - 1]) inc = false;
if (nums[i] > nums[i - 1]) dec = false;
}
return inc || dec;
}JavaScript
var isMonotonic = function(nums) {
let inc = true, dec = true;
for (let i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) inc = false;
if (nums[i] > nums[i - 1]) dec = false;
}
return inc || dec;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays