932. Monotonic Array
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a given array nums is monotonic. An array is monotonic if it is either monotone increasing or monotone decreasing. A key part of the definition is that equality is allowed. For example, [1, 2, 2, 3] is monotone increasing, and [6, 5, 5, 4] is monotone decreasing. An array with one or zero elements is trivially monotonic.
Solution
Monotonic Array in Python
class Solution:
def isMonotonic(self, nums: list[int]) -> bool:
# Assume the array is both increasing and decreasing initially.
increasing = True
decreasing = True
# Iterate through the array starting from the second element.
for i in range(1, len(nums)):
# If an element is smaller than its predecessor,
# it cannot be a non-decreasing (increasing) array.
if nums[i] < nums[i - 1]:
increasing = False
# If an element is larger than its predecessor,
# it cannot be a non-increasing (decreasing) array.
if nums[i] > nums[i - 1]:
decreasing = False
# A small optimization: if both flags are false, we can exit early.
if not increasing and not decreasing:
return False
# The array is monotonic if it's either non-decreasing or non-increasing.
return increasing or decreasing
Monotonic Array in Java
class Solution {
public boolean isMonotonic(int[] nums) {
// If the array has 2 or fewer elements, it's always monotonic.
// The loop logic below handles this correctly, but this is a common explicit check.
if (nums.length <= 2) {
return true;
}
// Assume the array is both increasing and decreasing initially.
boolean increasing = true;
boolean decreasing = true;
// Iterate through the array starting from the second element.
for (int i = 1; i < nums.length; i++) {
// If an element is smaller than its predecessor,
// it cannot be a non-decreasing (increasing) array.
if (nums[i] < nums[i - 1]) {
increasing = false;
}
// If an element is larger than its predecessor,
// it cannot be a non-increasing (decreasing) array.
if (nums[i] > nums[i - 1]) {
decreasing = false;
}
// A small optimization: if both flags are false, we can exit early.
if (!increasing && !decreasing) {
return false;
}
}
// The array is monotonic if it's either non-decreasing or non-increasing.
return increasing || decreasing;
}
}
Monotonic Array in C++
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
// If the array has 2 or fewer elements, it's always monotonic.
// The loop logic below handles this correctly, but this is a common explicit check.
if (nums.size() <= 2) {
return true;
}
// Assume the array is both increasing and decreasing initially.
bool increasing = true;
bool decreasing = true;
// Iterate through the array starting from the second element.
for (int i = 1; i < nums.size(); ++i) {
// If an element is smaller than its predecessor,
// it cannot be a non-decreasing (increasing) array.
if (nums[i] < nums[i-1]) {
increasing = false;
}
// If an element is larger than its predecessor,
// it cannot be a non-increasing (decreasing) array.
if (nums[i] > nums[i-1]) {
decreasing = false;
}
// A small optimization: if both flags are false, we can exit early.
if (!increasing && !decreasing) {
return false;
}
}
// The array is monotonic if it's either non-decreasing or non-increasing.
return increasing || decreasing;
}
};
Monotonic Array in TypeScript
function isMonotonic(nums: number[]): boolean {
// If the array has 2 or fewer elements, it's always monotonic.
// The loop logic below handles this correctly, but this is a common explicit check.
if (nums.length <= 2) {
return true;
}
// Assume the array is both increasing and decreasing initially.
let increasing = true;
let decreasing = true;
// Iterate through the array starting from the second element.
for (let i = 1; i < nums.length; i++) {
// If an element is smaller than its predecessor,
// it cannot be a non-decreasing (increasing) array.
if (nums[i] < nums[i - 1]) {
increasing = false;
}
// If an element is larger than its predecessor,
// it cannot be a non-increasing (decreasing) array.
if (nums[i] > nums[i - 1]) {
decreasing = false;
}
// A small optimization: if both flags are false, we can exit early.
if (!increasing && !decreasing) {
return false;
}
}
// The array is monotonic if it's either non-decreasing or non-increasing.
return increasing || decreasing;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.