- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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
- Java Program to Check if An Array Contains the Given Value
- Python program to check if a given string is number Palindrome
- Python program to check if a given string is Keyword or not
- Java Program to Check if An Array Contains a Given Value
- Golang Program to Check if An Array Contains a Given Value
- Python Program to check if two given matrices are identical
- Python Program to check if a substring is present in a given string.
- Python program to check if the string is pangram
- Find element position in given monotonic sequence in Python
- Check if the given number K is enough to reach the end of an array in Python
- Check if the array is beautiful in Python
