Check if given Array is Monotonic
Given an array arr[] containing N integers, the task is to check whether the array is monotonic or not (monotonic means either the array is in increasing order or in decreasing order).
Examples:
Input: arr[] = {1, 2, 2, 3}
Output: Yes
Explanation: Here 1 < 2 <= 2 < 3.
The array is in increasing order. Therefore it is monotonic.Input: arr[] = {6, 5, 4, 3}
Output: Yes
Explanation: Here 6 > 5 > 4 > 3.
The array is in decreasing order. So it is monotonic.Input: arr[] = {1, 5, 2}
Output: No
Explanation: Here 1 < 5 > 2. The array is neither increasing nor decreasing.
So the array is not monotonic
Approach: The problem can be solved by checking if the array is in increasing order or in decreasing order. This can be easily done in the following way:
- If for each i in range [0, N-2], arr[i] ≥ arr[i+1] the array is in decreasing order.
- If for each i in range [0, N-2], arr[i] ≤ arr[i+1], the array is in increasing order.
Follow the below steps to solve the problem:
- Traverse the array arr[] from i = 0 to N-2 and check if the array is increasing in order
- Traverse the array arr[] from i = 0 to N-2 and check if the array is decreasing in order
- If neither of the above two is true, then the array is not monotonic.
Below is the implementation of the above approach:
C++
// C++ program for aboe approach#include <bits/stdc++.h>using namespace std;// Function to check array is monotonicbool check(vector<int>& arr){ int N = arr.size(); bool inc = true; bool dec = true; // Loop to check if array is increasing for (int i = 0; i < N - 1; i++) { // To check if // array is not increasing if (arr[i] > arr[i + 1]) { inc = false; } } // Loop to check if array is decreasing for (int i = 0; i < N - 1; i++) { // To check if // array is not decreasing if (arr[i] < arr[i + 1]) { dec = false; } } // Pick one whether inc or dec return inc || dec;}// Driver codeint main(){ vector<int> arr = { 1, 2, 3, 3 }; // Function call bool ans = check(arr); if (ans) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java program for aboe approachimport java.io.*;class GFG { // Function to check array is monotonic static boolean check(int arr[]) { int N = arr.length; boolean inc = true; boolean dec = true; // Loop to check if array is increasing for (int i = 0; i < N - 1; i++) { // To check if // array is not increasing if (arr[i] > arr[i + 1]) { inc = false; } } // Loop to check if array is decreasing for (int i = 0; i < N - 1; i++) { // To check if // array is not decreasing if (arr[i] < arr[i + 1]) { dec = false; } } // Pick one whether inc or dec return inc || dec; } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 3 }; // Function call boolean ans = check(arr); if (ans == true) System.out.print("Yes"); else System.out.print("No"); }}// This code is contributed by hrithikgarg03188. |
Javascript
<script> // JavaScript program for aboe approach // Function to check array is monotonic const check = (arr) => { let N = arr.length; let inc = true; let dec = true; // Loop to check if array is increasing for (let i = 0; i < N - 1; i++) { // To check if // array is not increasing if (arr[i] > arr[i + 1]) { inc = false; } } // Loop to check if array is decreasing for (let i = 0; i < N - 1; i++) { // To check if // array is not decreasing if (arr[i] < arr[i + 1]) { dec = false; } } // Pick one whether inc or dec return inc || dec; } // Driver code let arr = [1, 2, 3, 3]; // Function call let ans = check(arr); if (ans) document.write("Yes"); else document.write("No"); // This code is contributed by rakeshsahni</script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)

