Given an array **A** containing **n** integers. The task is to check whether the array is **Monotonic** or not. An array is monotonic if it is either **monotone increasing** or **monotone decreasing**.

An array A is monotone increasing if for all i <= j, **A[i] <= A[j]**. An array A is monotone decreasing if for all i <= j, **A[i] >= A[j]**.

Return “**True**” if the given array A is monotonic else return “**False**” (without quotes).

Examples:

Input : 6 5 4 4 Output : true Input : 5 15 20 10 Output : false

**Approach:**

An array is **monotonic** if and only if it is **monotone increasing**, or **monotone decreasing**. Since **p <= q** and **q <= r** implies **p <= r**. So we only need to check adjacent elements to determine if the array is monotone increasing (or decreasing), respectively. We can check each of these properties in one pass.

To check whether an array A is **monotone increasing**, we’ll check **A[i] <= A[i+1]** for all i indexing from 0 to len(A)-2. Similarly we can check for **monotone decreasing** where **A[i] >= A[i+1]** for all i indexing from 0 to len(A)-2.

**Note:** Array with **single element** can be considered to be both **monotonic increasing or decreasing**, hence returns “**True**“.

**Below is the implementation of above approach:**

## Python3

`# Python3 program to find sum in Nth group ` ` ` `# Check if given array is Monotonic ` `def` `isMonotonic(A): ` ` ` ` ` `return` `(` `all` `(A[i] <` `=` `A[i ` `+` `1` `] ` `for` `i ` `in` `range` `(` `len` `(A) ` `-` `1` `)) ` `or` ` ` `all` `(A[i] >` `=` `A[i ` `+` `1` `] ` `for` `i ` `in` `range` `(` `len` `(A) ` `-` `1` `))) ` ` ` `# Driver program ` `A ` `=` `[` `6` `, ` `5` `, ` `4` `, ` `4` `] ` ` ` `# Print required result ` `print` `(isMonotonic(A)) ` ` ` `# This code is written by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

Output:

True

**Time Complexity:** O(N), where N is the length of array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Python Program for How to check if a given number is Fibonacci number?
- Python program to check if given string is vowel Palindrome
- Python program to check if given string is pangram
- Python program to check if a given string is Keyword or not
- Python program to check if given value occurs atleast k times
- Python program to check if the given string is IPv4 or IPv6 or Invalid
- Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Check if given array can be made 0 with given operations performed any number of times
- Check if concatenation of any permutation of given list of arrays generates the given array
- Python Program for Efficient program to print all prime factors of a given number
- Python program to check if the list contains three consecutive common numbers in Python
- Check whether a given array is a k sorted array or not
- Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not
- Program to check if an array is bitonic or not
- Program to check if an array is palindrome or not using Recursion
- Program to check if an Array is Palindrome or not
- Program to check if an array is sorted or not (Iterative and Recursive)
- Queries to increment array elements in a given range by a given value for a given number of times
- Python Program to Check Prime Number
- Python Program for Check if count of divisors is even or odd

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.