Check if an array is increasing or decreasing - GeeksforGeeks

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.
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: IncreasingInput: 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.
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to check the type of the arrayvoid checkType(int arr[], int n){     // If the first two and the last two elements    // of the array are in increasing order    if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])        cout << "Increasing";     // If the first two and the last two elements    // of the array are in decreasing order    else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])        cout << "Decreasing";     // If the first two elements of the array are in    // increasing order and the last two elements    // of the array are in decreasing order    else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])        cout << "Increasing then decreasing";     // If the first two elements of the array are in    // decreasing order and the last two elements    // of the array are in increasing order    else        cout << "Decreasing then increasing";} // Driver codeint main(){    int arr[] = { 1, 2, 3, 4 };    int n = sizeof(arr) / sizeof(arr[0]);     checkType(arr, n);     return 0;}
// Java implementation of the approachimport java.math.*; class GFG{     // Function to check the type of the array    public static void checkType(int arr[], int n)    {             // If the first two and the last two elements        // of the array are in increasing order        if (arr[0] <= arr[1] &&            arr[n - 2] <= arr[n - 1])            System.out.println("Increasing");             // If the first two and the last two elements        // of the array are in decreasing order        else if (arr[0] >= arr[1] &&                 arr[n - 2] >= arr[n - 1])            System.out.println("Decreasing");             // If the first two elements of the array are in        // increasing order and the last two elements        // of the array are in decreasing order        else if (arr[0] <= arr[1] &&                 arr[n - 2] >= arr[n - 1])            System.out.println("Increasing then decreasing");         // If the first two elements of the array are in        // decreasing order and the last two elements        // of the array are in increasing order        else            System.out.println("Decreasing then increasing");    }              // Driver code    public static void main(String[] args)    {        int[] arr = new int[]{ 1, 2, 3, 4 };                 int n = arr.length;         checkType(arr, n);    }} // This code is contributed by Naman_Garg
# Python3 implementation of the approach # Function to check the type of the arraydef checkType(arr, n):     # If the first two and the last two elements    # of the array are in increasing order    if (arr[0] <= arr[1] and        arr[n - 2] <= arr[n - 1]) :        print("Increasing");     # If the first two and the last two elements    # of the array are in decreasing order    elif (arr[0] >= arr[1] and          arr[n - 2] >= arr[n - 1]) :        print("Decreasing");     # If the first two elements of the array are in    # increasing order and the last two elements    # of the array are in decreasing order    elif (arr[0] <= arr[1] and          arr[n - 2] >= arr[n - 1]) :        print("Increasing then decreasing");     # If the first two elements of the array are in    # decreasing order and the last two elements    # of the array are in increasing order    else :        print("Decreasing then increasing"); # Driver codeif __name__ == "__main__" :     arr = [ 1, 2, 3, 4 ];    n = len(arr);     checkType(arr, n); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;class GFG{         // Function to check the type of the array    public static void checkType(int []arr, int n)    {             // If the first two and the last two elements        // of the array are in increasing order        if (arr[0] <= arr[1] &&            arr[n - 2] <= arr[n - 1])            Console.Write("Increasing");             // If the first two and the last two elements        // of the array are in decreasing order        else if (arr[0] >= arr[1] &&                 arr[n - 2] >= arr[n - 1])            Console.Write("Decreasing");             // If the first two elements of the array are in        // increasing order and the last two elements        // of the array are in decreasing order        else if (arr[0] <= arr[1] &&                 arr[n - 2] >= arr[n - 1])            Console.Write("Increasing then decreasing");         // If the first two elements of the array are in        // decreasing order and the last two elements        // of the array are in increasing order        else            Console.Write("Decreasing then increasing");    }     // Driver code    static public void Main ()    {        int[] arr = new int[]{ 1, 2, 3, 4 };                 int n = arr.Length;         checkType(arr, n);    }} // This code is contributed by ajit
<script> // Javascript implementation of the approach // Function to check the type of the arrayfunction checkType(arr, n){     // If the first two and the last two elements    // of the array are in increasing order    if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])        document.write("Increasing");     // If the first two and the last two elements    // of the array are in decreasing order    else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])        document.write("Decreasing");     // If the first two elements of the array are in    // increasing order and the last two elements    // of the array are in decreasing order    else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])        document.write("Increasing then decreasing");     // If the first two elements of the array are in    // decreasing order and the last two elements    // of the array are in increasing order    else        document.write("Decreasing then increasing");} // Driver code    let arr = [ 1, 2, 3, 4 ];    let n = arr.length;     checkType(arr, n); </script>
Increasing
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ankthon
Naman_Garg
jit_t
subhammahato348
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7