Check if an array is increasing or decreasing
Last Updated :
31 Mar, 2022
Given an array arr[] of N elements where N ? 2, the task is to check the type of array whether it is:
- Increasing.
- Decreasing.
- Increasing then decreasing.
- Decreasing then increasing.
Note that the given array is definitely one of the given types.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: Increasing
Input: arr[] = {1, 2, 4, 3}
Output: Increasing then decreasing
Approach: The following conditions must satisfy for:
- Increasing array: The first two and the last two elements must be in increasing order.
- Decreasing array: The first two and the last two elements must be in decreasing order.
- Increasing then decreasing array: The first two elements must be in increasing order and the last two elements must be in decreasing order.
- Decreasing then increasing array: The first two elements must be in decreasing order and the last two elements must be in increasing order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkType( int arr[], int n)
{
if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])
cout << "Increasing" ;
else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])
cout << "Decreasing" ;
else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])
cout << "Increasing then decreasing" ;
else
cout << "Decreasing then increasing" ;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
checkType(arr, n);
return 0;
}
|
Java
import java.math.*;
class GFG
{
public static void checkType( int arr[], int n)
{
if (arr[ 0 ] <= arr[ 1 ] &&
arr[n - 2 ] <= arr[n - 1 ])
System.out.println( "Increasing" );
else if (arr[ 0 ] >= arr[ 1 ] &&
arr[n - 2 ] >= arr[n - 1 ])
System.out.println( "Decreasing" );
else if (arr[ 0 ] <= arr[ 1 ] &&
arr[n - 2 ] >= arr[n - 1 ])
System.out.println( "Increasing then decreasing" );
else
System.out.println( "Decreasing then increasing" );
}
public static void main(String[] args)
{
int [] arr = new int []{ 1 , 2 , 3 , 4 };
int n = arr.length;
checkType(arr, n);
}
}
|
Python3
def checkType(arr, n):
if (arr[ 0 ] < = arr[ 1 ] and
arr[n - 2 ] < = arr[n - 1 ]) :
print ( "Increasing" );
elif (arr[ 0 ] > = arr[ 1 ] and
arr[n - 2 ] > = arr[n - 1 ]) :
print ( "Decreasing" );
elif (arr[ 0 ] < = arr[ 1 ] and
arr[n - 2 ] > = arr[n - 1 ]) :
print ( "Increasing then decreasing" );
else :
print ( "Decreasing then increasing" );
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ];
n = len (arr);
checkType(arr, n);
|
C#
using System;
class GFG
{
public static void checkType( int []arr, int n)
{
if (arr[0] <= arr[1] &&
arr[n - 2] <= arr[n - 1])
Console.Write( "Increasing" );
else if (arr[0] >= arr[1] &&
arr[n - 2] >= arr[n - 1])
Console.Write( "Decreasing" );
else if (arr[0] <= arr[1] &&
arr[n - 2] >= arr[n - 1])
Console.Write( "Increasing then decreasing" );
else
Console.Write( "Decreasing then increasing" );
}
static public void Main ()
{
int [] arr = new int []{ 1, 2, 3, 4 };
int n = arr.Length;
checkType(arr, n);
}
}
|
Javascript
<script>
function checkType(arr, n)
{
if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])
document.write( "Increasing" );
else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])
document.write( "Decreasing" );
else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])
document.write( "Increasing then decreasing" );
else
document.write( "Decreasing then increasing" );
}
let arr = [ 1, 2, 3, 4 ];
let n = arr.length;
checkType(arr, n);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...