Check if an array is increasing or decreasing

Given an array arr[] of N elements where N ≥ 2, the task is to check the type of array whether it is:

  1. Increasing.
  2. Decreasing.
  3. Increasing then decreasing.
  4. 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 satitisfy for:

  1. Increasing array: The first two and the last two elements must be in increasing order.
  2. Decreasing array: The first two and the last two elements must be in decreasing order.
  3. Increasing then decreasing array: The first two elements must be in increasing order and the last two elements must be in decreasing order.
  4. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to check the type of the array
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])
        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 code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    checkType(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
import 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to check the type of the array 
def 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 code 
if __name__ == "__main__"
  
    arr = [ 1, 2, 3, 4 ]; 
    n = len(arr); 
  
    checkType(arr, n); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using 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

chevron_right


Output:

Increasing


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Naman_Garg, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.