Check if Array forms an increasing-decreasing sequence or vice versa
Last Updated :
30 Oct, 2023
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”.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canMake( int n, int ar[])
{
if (n == 1)
return false ;
else {
if (ar[0] < ar[1]) {
int i = 1;
while (i < n
&& ar[i - 1] < ar[i]) {
i++;
}
while (i + 1 < n
&& ar[i] > ar[i + 1]) {
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else if (ar[0] > ar[1]) {
int i = 1;
while (i < n
&& ar[i - 1] > ar[i]) {
i++;
}
while (i + 1 < n
&& ar[i] < ar[i + 1]) {
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else {
for ( int i = 2; i < n; i++) {
if (ar[i - 1] <= ar[i])
return false ;
}
return true ;
}
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof arr / sizeof arr[0];
if (canMake(n, arr)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean canMake( int n, int ar[])
{
if (n == 1 )
return false ;
else
{
if (ar[ 0 ] < ar[ 1 ])
{
int i = 1 ;
while (i < n && ar[i - 1 ] < ar[i])
{
i++;
}
while (i + 1 < n && ar[i] > ar[i + 1 ])
{
i++;
}
if (i >= n - 1 )
return true ;
else
return false ;
}
else if (ar[ 0 ] > ar[ 1 ])
{
int i = 1 ;
while (i < n && ar[i - 1 ] > ar[i])
{
i++;
}
while (i + 1 < n && ar[i] < ar[i + 1 ])
{
i++;
}
if (i >= n - 1 )
return true ;
else
return false ;
}
else
{
for ( int i = 2 ; i < n; i++)
{
if (ar[i - 1 ] <= ar[i])
return false ;
}
return true ;
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
if (!canMake(n, arr)) {
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def canMake(n, ar):
if (n = = 1 ):
return False ;
else :
if (ar[ 0 ] < ar[ 1 ]):
i = 1 ;
while (i < n and ar[i - 1 ] < ar[i]):
i + = 1 ;
while (i + 1 < n and ar[i] > ar[i + 1 ]):
i + = 1 ;
if (i > = n - 1 ):
return True ;
else :
return False ;
elif (ar[ 0 ] > ar[ 1 ]):
i = 1 ;
while (i < n and ar[i - 1 ] > ar[i]):
i + = 1 ;
while (i + 1 < n and ar[i] < ar[i + 1 ]):
i + = 1 ;
if (i > = n - 1 ):
return True ;
else :
return False ;
else :
for i in range ( 2 , n):
if (ar[i - 1 ] < = ar[i]):
return False ;
return True ;
arr = [ 1 , 2 , 3 , 4 , 5 ];
n = len (arr);
if (canMake(n, arr) = = False ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool canMake( int n, int []ar)
{
if (n == 1)
return false ;
else
{
if (ar[0] < ar[1])
{
int i = 1;
while (i < n && ar[i - 1] < ar[i])
{
i++;
}
while (i + 1 < n && ar[i] > ar[i + 1])
{
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else if (ar[0] > ar[1])
{
int i = 1;
while (i < n && ar[i - 1] > ar[i])
{
i++;
}
while (i + 1 < n && ar[i] < ar[i + 1])
{
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else
{
for ( int i = 2; i < n; i++)
{
if (ar[i - 1] <= ar[i])
return false ;
}
return true ;
}
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
if (!canMake(n, arr))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function canMake(n, ar)
{
if (n == 1)
return false ;
else
{
if (ar[0] < ar[1])
{
let i = 1;
while (i < n && ar[i - 1] < ar[i])
{
i++;
}
while (i + 1 < n && ar[i] > ar[i + 1])
{
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else if (ar[0] > ar[1])
{
let i = 1;
while (i < n && ar[i - 1] > ar[i])
{
i++;
}
while (i + 1 < n && ar[i] < ar[i + 1])
{
i++;
}
if (i >= n - 1)
return true ;
else
return false ;
}
else
{
for (let i = 2; i < n; i++)
{
if (ar[i - 1] <= ar[i])
return false ;
}
return true ;
}
}
}
let arr = [ 1, 2, 3, 4, 5 ];
let n = arr.length;
if (!canMake(n, arr))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Method3: Simple and efficient approach
Approach: we create a function divide array and iterates in the array and uses two flags increasing and decreasing to track if an increasing and decreasing sub-array has been found. If both flags are true at any point in the iteration, then we print yes and If both flags are not true after iterating through the entire array, then we print false.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string Divide_Array( int arr[], int N) {
bool increasing = false ;
bool decreasing = false ;
for ( int i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) {
increasing = true ;
} else if (arr[i] < arr[i - 1]) {
decreasing = true ;
}
if (increasing && decreasing) {
return "Yes" ;
}
}
return "No" ;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5 };
int N= sizeof arr / sizeof arr[0];
string result = Divide_Array(arr, N);
cout << result << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static String divideArray( int [] arr, int N) {
boolean increasing = false ;
boolean decreasing = false ;
for ( int i = 1 ; i < N; i++) {
if (arr[i] > arr[i - 1 ]) {
increasing = true ;
} else if (arr[i] < arr[i - 1 ]) {
decreasing = true ;
}
if (increasing && decreasing) {
return "Yes" ;
}
}
return "No" ;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
String result = divideArray(arr, N);
System.out.println(result);
}
}
|
Python3
def divide_array(arr):
increasing = False
decreasing = False
for i in range ( 1 , len (arr)):
if arr[i] > arr[i - 1 ]:
increasing = True
elif arr[i] < arr[i - 1 ]:
decreasing = True
if increasing and decreasing:
return "Yes"
return "No"
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
result = divide_array(arr)
print (result)
|
C#
using System;
class Program {
static string DivideArray( int [] arr, int N)
{
bool increasing
= false ;
bool decreasing
= false ;
for ( int i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) {
increasing = true ;
}
else if (arr[i] < arr[i - 1]) {
decreasing = true ;
}
if (increasing && decreasing) {
return "Yes" ;
}
}
return "No" ;
}
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
string result = DivideArray(arr, N);
Console.WriteLine(result);
Console.ReadKey();
}
}
|
Javascript
function divideArray(arr) {
let increasing = false ;
let decreasing = false ;
for (let i = 1; i < arr.length; i++) {
if (arr[i] > arr[i - 1]) {
increasing = true ;
} else if (arr[i] < arr[i - 1]) {
decreasing = true ;
}
if (increasing && decreasing) {
return "Yes" ;
}
}
return "No" ;
}
function main() {
const arr = [1, 2, 3, 4, 5];
const result = divideArray(arr);
console.log(result);
}
main();
|
Time Complexity: O(N), where N is the size of an array
Auxiliary Space: O(1), as we are not using any extra space .
Share your thoughts in the comments
Please Login to comment...