Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to check if the given array is Monotonic
In this article, we will learn how to check if a given array is monotonic in nature. An array is monotonic if it is either continuously increasing or continuously decreasing.
Problem Statement
Given an array containing n integers, we need to determine whether the input array is monotonic or not.
An array is said to be monotonic if it satisfies one of these conditions:
- Monotonic Increasing: For all indices i ? j, A[i] ? A[j]
- Monotonic Decreasing: For all indices i ? j, A[i] ? A[j]
Approach
We check if all adjacent elements satisfy either the increasing condition (A[i] ? A[i+1]) or the decreasing condition (A[i] ? A[i+1]) throughout the entire array.
Using Built-in all() Function
The most concise approach uses Python's all() function to check both conditions ?
def isMonotonic(arr):
return (all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1)) or
all(arr[i] >= arr[i + 1] for i in range(len(arr) - 1)))
# Test with increasing array
arr1 = [1, 2, 3, 4, 7, 8]
print(f"Array {arr1} is monotonic: {isMonotonic(arr1)}")
# Test with decreasing array
arr2 = [8, 7, 4, 3, 2, 1]
print(f"Array {arr2} is monotonic: {isMonotonic(arr2)}")
# Test with non-monotonic array
arr3 = [1, 3, 2, 4]
print(f"Array {arr3} is monotonic: {isMonotonic(arr3)}")
Array [1, 2, 3, 4, 7, 8] is monotonic: True Array [8, 7, 4, 3, 2, 1] is monotonic: True Array [1, 3, 2, 4] is monotonic: False
Using Single Pass Method
A more efficient approach that scans the array only once ?
def isMonotonicSinglePass(arr):
if len(arr) <= 1:
return True
increasing = decreasing = True
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
increasing = False
if arr[i] > arr[i-1]:
decreasing = False
# Early exit if neither condition holds
if not increasing and not decreasing:
return False
return True
# Test cases
test_arrays = [
[1, 2, 2, 3], # Monotonic increasing
[6, 5, 4, 4], # Monotonic decreasing
[1, 3, 2, 4, 5], # Not monotonic
[1, 1, 1, 1] # Monotonic (equal elements)
]
for arr in test_arrays:
result = isMonotonicSinglePass(arr)
print(f"Array {arr} is monotonic: {result}")
Array [1, 2, 2, 3] is monotonic: True Array [6, 5, 4, 4] is monotonic: True Array [1, 3, 2, 4, 5] is monotonic: False Array [1, 1, 1, 1] is monotonic: True
Comparison
| Method | Time Complexity | Space Complexity | Early Exit |
|---|---|---|---|
all() Function |
O(n) | O(1) | Yes (built-in) |
| Single Pass | O(n) | O(1) | Yes (manual) |
Edge Cases
Consider these special cases when implementing monotonic array check ?
def testEdgeCases():
edge_cases = [
[], # Empty array
[5], # Single element
[1, 1], # Two equal elements
[1, 2], # Two increasing elements
[2, 1] # Two decreasing elements
]
for arr in edge_cases:
result = isMonotonic(arr) if arr else True # Empty arrays are monotonic
print(f"Array {arr}: {result}")
testEdgeCases()
Array []: True Array [5]: True Array [1, 1]: True Array [1, 2]: True Array [2, 1]: True
Conclusion
Both approaches effectively determine if an array is monotonic. The all() function provides cleaner code, while the single-pass method offers explicit control over early termination. Both have O(n) time complexity and work well for practical applications.
