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++
// 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;
}
Java
// 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
Python3
# 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
C#
// 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
JavaScript
<script>
// Javascript implementation of the approach
// Function to check the type of the array
function 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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Increasing Decreasing Update Queries (Akku and Arrays) Akku has solved many problems, she is a genius. One day her friend gave her an Array of sizes n and asked her to perform some queries of the following type: Each query consists of three integers 1 A B: Update the Array at index A by value B2 A B: if the subarray from index A to B (both inclusive) is
15+ min read
Check if it is possible to make array increasing or decreasing by rotating the array Given an array arr[] of N distinct elements, the task is to check if it is possible to make the array increasing or decreasing by rotating the array in any direction.Examples: Input: arr[] = {4, 5, 6, 2, 3} Output: Yes Array can be rotated as {2, 3, 4, 5, 6}Input: arr[] = {1, 2, 4, 3, 5} Output: No
13 min read
Check if given Array can be formed by increasing or decreasing element Given an array arr[] of length N with N integers. Assume all the elements of array arr[] are equal to 0 and start with the first element. The task is to obtain the given array after performing the given operations on it. At last, you must be there from where you start. Return 1 if it is possible to
7 min read
Operations to Sort an Array in non-decreasing order Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ⤠arr[i+1]) by using two types of operation by performing any numbers of time: You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1]
7 min read
Check if a given array is pairwise sorted or not An array is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In case of odd elements, last element is ignored and result is based on remaining even number of elements. Examples: Input : arr[] = {10, 15, 9, 9, 1, 5}; Output : Yes Pairs are (10, 15), (
5 min read