

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to check if the given array is Monotonic
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given an array input Arr containing n integers. We need to check whether the input array is Monotonic in nature or not.
An array is said to be monotonic in nature if it is either continuously increasing or continuously decreasing.
Mathematically,
An array A is continuously increasing if for all i <= j,
A[i] <= A[j].
An array A is continuously decreasing if for all i <= j,
A[i] >= A[j].
Here we will check whether all the adjacent elements satisfy one of the above conditions or not.
Now let’s see the implementstion −
Example
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))) # main A = [1,2,3,4,7,8] print(isMonotonic(A))
Output
True
All the variables are declared in the global frame as shown in the figure given below −
Conclusion
In this article, we learnt about the approach to find whether an array is monotonic in nature or not
- Related Questions & Answers
- Python program to check if the given string is pangram
- Python program to check if the given string is vowel Palindrome
- Python program to check if the given number is Happy Number
- Python program to check if the given number is a Disarium Number
- Python program to check if a given string is number Palindrome
- Java Program to Check if An Array Contains the Given Value
- Python Pandas - Check if the given DateOffset is Anchored
- Python Pandas - Check if the given CustomBusinessHour is Anchored
- Check if the array is beautiful in Python
- Python program to check if a given string is Keyword or not
- Python program to check if the string is pangram
- Python Program to check if a substring is present in a given string.
- Java Program to Check if An Array Contains a Given Value
- Python Program to check if two given matrices are identical
- Check if the given number K is enough to reach the end of an array in Python