Check if Array forms an increasing-decreasing sequence or vice versa - GeeksforGeeks

01 Apr, 2022
Given an array arr[] of N integers, the task is to find if the array can be divided into 2 sub-array such that the first sub-array is strictly increasing and the second sub-array is strictly decreasing or vice versa. If the given array can be divided then print “Yes” else print “No”.
Examples: 
Input: arr[] = {3, 1, -2, -2, -1, 3} Output: Yes Explanation: First sub-array {3, 1, -2} which is strictly decreasing and second sub-array is {-2, 1, 3} is strictly increasing.
Input: arr[] = {1, 1, 2, 3, 4, 5} Output: No Explanation: The entire array is increasing.  
Naive Approach: The naive idea is to divide the array into two subarrays at every possible index and explicitly check if the first subarray is strictly increasing and the second subarray is strictly decreasing or vice-versa. If we can break any subarray then print “Yes” else print “No”. Time Complexity: O(N2) Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, traverse the array and check for the strictly increasing sequence and then check for strictly decreasing subsequence or vice-versa. Below are the steps: 
If arr[1] > arr[0], then check for strictly increasing then strictly decreasing as: Check for every consecutive pair until at any index i arr[i + 1] is less than arr[i].Now from index i + 1 check for every consecutive pair check if arr[i + 1] is less than arr[i] till the end of the array or not. If at any index i, arr[i] is less than arr[i + 1] then break the loop.If we reach the end in the above step then print “Yes” Else print “No”.If arr[1] < arr[0], then check for strictly decreasing then strictly increasing as: Check for every consecutive pair until at any index i arr[i + 1] is greater than arr[i].Now from index i + 1 check for every consecutive pair check if arr[i + 1] is greater than arr[i] till the end of the array or not. If at any index i, arr[i] is greater than arr[i + 1] then break the loop.If we reach the end in the above step then print “Yes” Else print “No”.
If arr[1] > arr[0], then check for strictly increasing then strictly decreasing as: Check for every consecutive pair until at any index i arr[i + 1] is less than arr[i].Now from index i + 1 check for every consecutive pair check if arr[i + 1] is less than arr[i] till the end of the array or not. If at any index i, arr[i] is less than arr[i + 1] then break the loop.If we reach the end in the above step then print “Yes” Else print “No”.
Check for every consecutive pair until at any index i arr[i + 1] is less than arr[i].
Now from index i + 1 check for every consecutive pair check if arr[i + 1] is less than arr[i] till the end of the array or not. If at any index i, arr[i] is less than arr[i + 1] then break the loop.
If we reach the end in the above step then print “Yes” Else print “No”.
If arr[1] < arr[0], then check for strictly decreasing then strictly increasing as: Check for every consecutive pair until at any index i arr[i + 1] is greater than arr[i].Now from index i + 1 check for every consecutive pair check if arr[i + 1] is greater than arr[i] till the end of the array or not. If at any index i, arr[i] is greater than arr[i + 1] then break the loop.If we reach the end in the above step then print “Yes” Else print “No”.
Check for every consecutive pair until at any index i arr[i + 1] is greater than arr[i].
Now from index i + 1 check for every consecutive pair check if arr[i + 1] is greater than arr[i] till the end of the array or not. If at any index i, arr[i] is greater than arr[i + 1] then break the loop.
If we reach the end in the above step then print “Yes” Else print “No”.
Below is the implementation of above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if the given array// forms an increasing decreasing// sequence or vice versabool canMake(int n, int ar[]){    // Base Case    if (n == 1)        return true;    else {         // First subarray is        // strictly increasing        if (ar[0] < ar[1]) {             int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n                   && ar[i - 1] < ar[i]) {                i++;            }             // Check for strictly            // decreasing condition            // & find the break point            while (i + 1 < n                   && ar[i] > ar[i + 1]) {                i++;            }             // If i is equal to            // length of array            if (i >= n - 1)                return true;            else                return false;        }         // First subarray is        // strictly Decreasing        else if (ar[0] > ar[1]) {            int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n                   && ar[i - 1] > ar[i]) {                i++;            }             // Check for strictly            // increasing condition            // & find the break point            while (i + 1 < n                   && ar[i] < ar[i + 1]) {                i++;            }             // If i is equal to            // length of array - 1            if (i >= n - 1)                return true;            else                return false;        }         // Condition if ar[0] == ar[1]        else {            for (int i = 2; i < n; i++) {                if (ar[i - 1] <= ar[i])                    return false;            }            return true;        }    }} // Driver Codeint main(){    // Given array arr[]    int arr[] = { 1, 2, 3, 4, 5 };    int n = sizeof arr / sizeof arr[0];     // Function Call    if (canMake(n, arr)) {        cout << "Yes";    }    else {        cout << "No";    }     return 0;}
// Java program for the above approachimport java.util.*;class GFG{ // Function to check if the given array// forms an increasing decreasing// sequence or vice versastatic boolean canMake(int n, int ar[]){    // Base Case    if (n == 1)        return true;    else    {         // First subarray is        // strictly increasing        if (ar[0] < ar[1])        {             int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] < ar[i])            {                i++;            }             // Check for strictly            // decreasing condition            // & find the break point            while (i + 1 < n && ar[i] > ar[i + 1])            {                i++;            }             // If i is equal to            // length of array            if (i >= n - 1)                return true;            else                return false;        }         // First subarray is        // strictly Decreasing        else if (ar[0] > ar[1])        {            int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] > ar[i])            {                i++;            }             // Check for strictly            // increasing condition            // & find the break point            while (i + 1 < n && ar[i] < ar[i + 1])            {                i++;            }             // If i is equal to            // length of array - 1            if (i >= n - 1)                return true;            else                return false;        }         // Condition if ar[0] == ar[1]        else        {            for (int i = 2; i < n; i++)            {                if (ar[i - 1] <= ar[i])                    return false;            }            return true;        }    }} // Driver Codepublic static void main(String[] args){    // Given array arr[]    int arr[] = { 1, 2, 3, 4, 5 };    int n = arr.length;     // Function Call    if (!canMake(n, arr)) {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by Rohit_ranjan
# Python3 program for the above approach # Function to check if the given array# forms an increasing decreasing# sequence or vice versadef canMake(n, ar):         # Base Case    if (n == 1):        return True;    else:         # First subarray is        # strictly increasing        if (ar[0] < ar[1]):             i = 1;             # Check for strictly            # increasing condition            # & find the break point            while (i < n and ar[i - 1] < ar[i]):                i += 1;             # Check for strictly            # decreasing condition            # & find the break point            while (i + 1 < n and ar[i] > ar[i + 1]):                i += 1;             # If i is equal to            # length of array            if (i >= n - 1):                return True;            else:                return False;         # First subarray is        # strictly Decreasing        elif (ar[0] > ar[1]):            i = 1;             # Check for strictly            # increasing condition            # & find the break point            while (i < n and ar[i - 1] > ar[i]):                i += 1;             # Check for strictly            # increasing condition            # & find the break point            while (i + 1 < n and ar[i] < ar[i + 1]):                i += 1;             # If i is equal to            # length of array - 1            if (i >= n - 1):                return True;            else:                return False;         # Condition if ar[0] == ar[1]        else:            for i in range(2, n):                if (ar[i - 1] <= ar[i]):                    return False;             return True; # Driver Code # Given array arrarr = [1, 2, 3, 4, 5];n = len(arr); # Function Callif (canMake(n, arr)==False):    print("Yes");else:    print("No"); # This code is contributed by PrinciRaj1992
// C# program for the above approachusing System;class GFG{ // Function to check if the given array// forms an increasing decreasing// sequence or vice versastatic bool canMake(int n, int []ar){    // Base Case    if (n == 1)        return true;    else    {         // First subarray is        // strictly increasing        if (ar[0] < ar[1])        {             int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] < ar[i])            {                i++;            }             // Check for strictly            // decreasing condition            // & find the break point            while (i + 1 < n && ar[i] > ar[i + 1])            {                i++;            }             // If i is equal to            // length of array            if (i >= n - 1)                return true;            else                return false;        }         // First subarray is        // strictly Decreasing        else if (ar[0] > ar[1])        {            int i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] > ar[i])            {                i++;            }             // Check for strictly            // increasing condition            // & find the break point            while (i + 1 < n && ar[i] < ar[i + 1])            {                i++;            }             // If i is equal to            // length of array - 1            if (i >= n - 1)                return true;            else                return false;        }         // Condition if ar[0] == ar[1]        else        {            for (int i = 2; i < n; i++)            {                if (ar[i - 1] <= ar[i])                    return false;            }            return true;        }    }} // Driver Codepublic static void Main(String[] args){    // Given array []arr    int []arr = { 1, 2, 3, 4, 5 };    int n = arr.Length;     // Function Call    if (!canMake(n, arr))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Rajput-Ji
<script> // Javascript program for the above approach // Function to check if the given array// forms an increasing decreasing// sequence or vice versafunction canMake(n, ar){         // Base Case    if (n == 1)        return true;    else    {                 // First subarray is        // strictly increasing        if (ar[0] < ar[1])        {            let i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] < ar[i])            {                i++;            }             // Check for strictly            // decreasing condition            // & find the break point            while (i + 1 < n && ar[i] > ar[i + 1])            {                i++;            }             // If i is equal to            // length of array            if (i >= n - 1)                return true;            else                return false;        }         // First subarray is        // strictly Decreasing        else if (ar[0] > ar[1])        {            let i = 1;             // Check for strictly            // increasing condition            // & find the break point            while (i < n && ar[i - 1] > ar[i])            {                i++;            }             // Check for strictly            // increasing condition            // & find the break point            while (i + 1 < n && ar[i] < ar[i + 1])            {                i++;            }             // If i is equal to            // length of array - 1            if (i >= n - 1)                return true;            else                return false;        }         // Condition if ar[0] == ar[1]        else        {            for(let i = 2; i < n; i++)            {                if (ar[i - 1] <= ar[i])                    return false;            }            return true;        }    }} // Driver Code // Given array arr[]let arr = [ 1, 2, 3, 4, 5 ];let n = arr.length; // Function Callif (!canMake(n, arr)){    document.write("Yes");}else{    document.write("No");} // This code is contributed by sravan kumar </script>
No
 
Time Complexity: O(N) Auxiliary Space: O(1) 
Rohit_ranjan
Rajput-Ji
princiraj1992
sravankumar8128
simranarora5sos
Algorithms-Greedy Algorithms
Greedy Algorithms
Arrays
Greedy
Arrays
Greedy
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
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Write a program to print all permutations of a given string
Huffman Coding | Greedy Algo-3