Python

Check if the given Array is Monotonic Python

We’ll learn how to determine whether an array is a monotone in this article. An array is a data structure that holds elements and allows you to retrieve them using their indexes. When the items of an array are constantly rising or decreasing, the array is said to be monotonic.

Monotonic increasing

An array A[] is monotonic increasing if all the elements in it satisfy the condition: 

for all i <= j, A[i] <= A[j]

[1, 2, 3, 4, 7, 10] is monotonic increasing.

Monotonic decreasing

An array A[] is monotone decreasing if all the elements in it satisfy the condition:

for all i <= j, A[i] >= A[j]

[11, 10, 9, 6, 4, 1] is monotonic decreasing.

The program should take the array as input and return True if the array is monotonic else it should return False. For example,

Look at the sample input and output for the program.

Input: 7 3 2 1 0

Output: True

Input: 10 11 13 9 14

Output: False

We’ll use the strategy of checking the nearby elements in the array to see if the array is monotonic rising or decreasing. To test for monotonic growth, we’ll see if a[i]= a[i+1] for all indices I from 0 to n-1, where n is the array’s size. To verify for monotonic decrease, we’ll see if a[i]>= a[i+1] for all indexes I between 0 and n-1, where n is the array’s size.

Note:

In our software, a monotone array with only one entry will be treated as such, and the method should return True.

Algorithm 

Examine the algorithm to gain a better understanding of how the programme works. 

Step 1: Create a method that checks the array’s monotone nature. 

Step 2: Using len, find and store the array’s size () 

Step 3: If there is only one element in the array, return True. 

Step 4: If not, see if all of the array’s values are consistently decreasing or growing. 

Step 5: Return True if the condition is true. 

Step 6: Return False if the condition is not true. 

Step 7: Create an array of values. 

Step 8: Call the function using the array. 

Step 9: Print the outcome

Python Program

This programme determines whether or not an array is a monotone. We’ve built a function that returns True if the array is monotone and False otherwise.

#check if monotone
#function definition
def ismonotone(a):
    n=len(a) #size of array
    if n==1:
        return True
    else:
        #check for monotone behaviour
        if all(a[i]>=a[i+1] for i in range(0,n-1) or a[i]<=a[i+1] for i in range(0,n-1)):
            return True
        else:
            return False

A = [6, 5, 4, 2]
print(ismonotone(A))
b = [6, 2, 4, 2]
print(ismonotone(b))
c=[4,3,2]
print(ismonotone(c))
d=[1]
print(ismonotone(d))

Output:

True
False
True
True

Conclusion

We covered what monotone arrays are and how to use a function to verify if an array is monotone or not in this course. Monotonic arrays are divided into two types: monotone increasing and monotone decreasing. A monotone array is one with only one element.

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *