Monotonic Array
Learn Monotonic Array (Arrays, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
What is a Monotonic Array? An array is monotonic if it is entirely non-increasing or entirely non-decreasing - values only move in one direction (or stay flat), never both. Monotonically increasing: arr[i] ≤ arr[i+1] for every i. Monotonically decreasing: arr[i] ≥ arr[i+1] for every i. Checking is a single O(n) pass: track whether any "increasing" violation and any "decreasing" violation occurs - the array is monotonic if at least one of the two never happens.