Check if given Array is Monotonic
Last Updated :
10 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool check(vector< int >& arr)
{
int N = arr.size();
bool inc = true ;
bool dec = true ;
for ( int i = 0; i < N - 1; i++) {
if (arr[i] > arr[i + 1]) {
inc = false ;
}
}
for ( int i = 0; i < N - 1; i++) {
if (arr[i] < arr[i + 1]) {
dec = false ;
}
}
return inc || dec;
}
int main()
{
vector< int > arr = { 1, 2, 3, 3 };
bool ans = check(arr);
if (ans)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean check( int arr[])
{
int N = arr.length;
boolean inc = true ;
boolean dec = true ;
for ( int i = 0 ; i < N - 1 ; i++) {
if (arr[i] > arr[i + 1 ]) {
inc = false ;
}
}
for ( int i = 0 ; i < N - 1 ; i++) {
if (arr[i] < arr[i + 1 ]) {
dec = false ;
}
}
return inc || dec;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 3 };
boolean ans = check(arr);
if (ans == true )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def check(arr):
N = len (arr)
inc = True
dec = True
for i in range ( 0 , N - 1 ):
if arr[i] > arr[i + 1 ]:
inc = False
for i in range ( 0 , N - 1 ):
if arr[i] < arr[i + 1 ]:
dec = False
return inc or dec
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 3 ]
ans = check(arr)
if ans = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool check( int [] arr)
{
int N = arr.Length;
bool inc = true ;
bool dec = true ;
for ( int i = 0; i < N - 1; i++) {
if (arr[i] > arr[i + 1]) {
inc = false ;
}
}
for ( int i = 0; i < N - 1; i++) {
if (arr[i] < arr[i + 1]) {
dec = false ;
}
}
return (inc || dec);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 3 };
bool ans = check(arr);
if (ans)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
const check = (arr) => {
let N = arr.length;
let inc = true ;
let dec = true ;
for (let i = 0; i < N - 1; i++) {
if (arr[i] > arr[i + 1]) {
inc = false ;
}
}
for (let i = 0; i < N - 1; i++) {
if (arr[i] < arr[i + 1]) {
dec = false ;
}
}
return inc || dec;
}
let arr = [1, 2, 3, 3];
let ans = check(arr);
if (ans)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach: (JavaScript) In this approach JavaScript’s array method, named as every() will be used and therefore we will further check using this method that the array passed is Monotonic in nature or not.
Follow the below mentioned steps:
- Traverse the array using every() method and check whether the current element is smaller than previous element.
- Similarly again using every() method as well as with the usage of Logical OR Operator ( | | ) check whether the current element is greater than the previous element or not.
- Return true if either of the increasing sequence or the decreasing sequence is found out.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <algorithm>
using namespace std;
bool check_monotonic( int array[], int size)
{
if (is_sorted(array, array + size, greater< int >()))
return true ;
if (is_sorted(array, array + size))
return true ;
return false ;
}
int main()
{
int array1[] = { 7, 5, 3, 1 };
int array2[] = { 4, 0, 3, 1 };
int array3[] = { 5, 4, 3 };
int size1 = sizeof (array1) / sizeof (array1[0]);
int size2 = sizeof (array2) / sizeof (array2[0]);
int size3 = sizeof (array3) / sizeof (array3[0]);
if (check_monotonic(array1, size1))
cout << "Is Monotonic ?: true\n" ;
else
cout << "Is Monotonic ?: false\n" ;
if (check_monotonic(array2, size2))
cout << "Is Monotonic ?: true\n" ;
else
cout << "Is Monotonic ?: false\n" ;
if (check_monotonic(array3, size3))
cout << "Is Monotonic ?: true\n" ;
else
cout << "Is Monotonic ?: false\n" ;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean checkMonotonic( int [] array) {
int size = array.length;
if (isSorted(array, size, true ))
return true ;
if (isSorted(array, size, false ))
return true ;
return false ;
}
static boolean isSorted( int [] array, int size, boolean isDescending) {
if (isDescending) {
for ( int i = 1 ; i < size; i++) {
if (array[i] > array[i - 1 ])
return false ;
}
} else {
for ( int i = 1 ; i < size; i++) {
if (array[i] < array[i - 1 ])
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int [] array1 = { 7 , 5 , 3 , 1 };
int [] array2 = { 4 , 0 , 3 , 1 };
int [] array3 = { 5 , 4 , 3 };
if (checkMonotonic(array1))
System.out.println( "Is Monotonic ?: true" );
else
System.out.println( "Is Monotonic ?: false" );
if (checkMonotonic(array2))
System.out.println( "Is Monotonic ?: true" );
else
System.out.println( "Is Monotonic ?: false" );
if (checkMonotonic(array3))
System.out.println( "Is Monotonic ?: true" );
else
System.out.println( "Is Monotonic ?: false" );
}
}
|
Python3
def check_monotonic(array):
return (
all (element < = array[index - 1 ] for index, element in enumerate (array) if index > 0 ) or
all (element > = array[index - 1 ] for index, element in enumerate (array) if index > 0 )
)
print ( "Is Monotonic ?: " , check_monotonic([ 7 , 5 , 3 , 1 ]))
print ( "Is Monotonic ?: " , check_monotonic([ 4 , 0 , 3 , 1 ]))
print ( "Is Monotonic ?: " , check_monotonic([ 5 , 4 , 3 ]))
|
Javascript
let checkMonotonic = (array) => {
return (
array.every(
(element, index) => index === 0 || element <= array[index - 1]
) ||
array.every((element, index) => index === 0 || element >= array[index - 1])
);
};
console.log( "Is Monotonic ?: " + checkMonotonic([7, 5, 3, 1]));
console.log( "Is Monotonic ?: " + checkMonotonic([4, 0, 3, 1]));
console.log( "Is Monotonic ?: " + checkMonotonic([5, 4, 3]));
|
C#
using System;
using System.Linq;
class Gfg {
static void Main() {
int [] array1 = { 7, 5, 3, 1 };
int [] array2 = { 4, 0, 3, 1 };
int [] array3 = { 5, 4, 3 };
Console.WriteLine( "Is Monotonic ?: " + checkMonotonic(array1));
Console.WriteLine( "Is Monotonic ?: " + checkMonotonic(array2));
Console.WriteLine( "Is Monotonic ?: " + checkMonotonic(array3));
}
static bool checkMonotonic( int [] array) {
return (
array.Take(1).Count() == array.Length ||
Enumerable.Range(1, array.Length - 1).All(i => array[i] <= array[i - 1])
||
Enumerable.Range(1, array.Length - 1).All(i => array[i] >= array[i - 1])
);
}
}
|
Output
Is Monotonic ?: true
Is Monotonic ?: false
Is Monotonic ?: true
Time Complexity: O(NlogN), as the time complexity of the is_sorted() function in the standard library is O(NlogN), where N is the size of the array.
Auxiliary Space: O(1), as it requires constant extra space for storing the size of the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...