Python Program to check if given array is Monotonic (original) (raw)

Last Updated : 4 Nov, 2025

Given an array of integers, the task is to check whether it is monotonic. A monotonic array is an array that consistently increases or decreases.

**Return: True if the array is monotonic, otherwise False.

**For Examples:

**Input: [6, 5, 4, 4] -> **Output: True
**Input: [5, 15, 20, 10] -> **Output: False

**Explanation: [6, 5, 4, 4] array is continuously decreasing, whereas [5, 15, 20, 10] array first increases then decreases.

Using Single Pass

This method checks whether the array is monotonic by comparing each adjacent pair of elements in a single pass. If all pairs are either in increasing or decreasing order, the array is monotonic.

Python `

arr = [6, 5, 4, 4] n = len(arr)

incr = all(arr[i] <= arr[i+1] for i in range(n-1)) decr = all(arr[i] >= arr[i+1] for i in range(n-1))

print(incr or decr)

`

**Explanation:

Using Direction Variable

In this approach, we determines the initial direction (increasing or decreasing) using the first two elements, then compares the rest of the array to ensure the direction never changes.

Python `

arr = [6, 5, 4, 4] n = len(arr)

if n <= 2: print(True) else: d = arr[1] - arr[0] m = True for i in range(2, n): if d == 0: d = arr[i] - arr[i-1] continue if (d > 0 and arr[i] < arr[i-1]) or (d < 0 and arr[i] > arr[i-1]): m = False break print(m)

`

**Explanation:

Using zip

Here, we use zip() to pair each element with its next one, making adjacent comparisons easy to check for increasing or decreasing order.

Python `

arr = [6, 5, 4, 4] incr = all(a <= b for a, b in zip(arr, arr[1:])) decr = all(a >= b for a, b in zip(arr, arr[1:])) print(incr or decr)

`

**Explanation:

Using Sorting

This method checks whether the array remains unchanged when sorted in ascending or descending order. If either matches, the array is monotonic.

Python `

arr = [6, 5, 4, 4] incr = sorted(arr) == arr decr = sorted(arr, reverse=True) == arr print(incr or decr)

`

**Explanation: