Monotonic Array - Problem
Imagine you're analyzing data trends - monotonic arrays are sequences that consistently move in one direction, never changing course!
An array is monotonic if it follows one of these patterns:
- Monotone Increasing: Each element is greater than or equal to the previous one (nums[i] ≤ nums[j] for all i ≤ j)
- Monotone Decreasing: Each element is less than or equal to the previous one (nums[i] ≥ nums[j] for all i ≤ j)
Given an integer array nums, determine if it maintains a monotonic pattern.
Examples:
[1, 2, 2, 3]→ true (increasing)[6, 5, 4, 4]→ true (decreasing)[1, 3, 2]→ false (changes direction)
Input & Output
example_1.py — Python
$
Input:
[1,2,2,3]
›
Output:
true
💡 Note:
The array is monotone increasing (non-decreasing). Each element is greater than or equal to the previous one.
example_2.py — Python
$
Input:
[6,5,4,4]
›
Output:
true
💡 Note:
The array is monotone decreasing (non-increasing). Each element is less than or equal to the previous one.
example_3.py — Python
$
Input:
[1,3,2]
›
Output:
false
💡 Note:
The array changes direction: increases from 1 to 3, then decreases from 3 to 2. It's neither monotone increasing nor decreasing.
Constraints
- 1 ≤ nums.length ≤ 105
- -105 ≤ nums[i] ≤ 105
- Edge cases: Single element or empty arrays are considered monotonic
Visualization
Tap to expand
Understanding the Visualization
1
Trend Detection
As we scan through daily prices, we note any increases or decreases
2
Pattern Recognition
If we see both upward AND downward movements, the stock is volatile (not monotonic)
3
Early Warning
The moment we detect mixed trends, we know it's not a consistent pattern
Key Takeaway
🎯 Key Insight: We only need to track if we've seen BOTH increasing and decreasing trends. If we see both, it's not monotonic - just like a volatile stock that goes both up and down!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code