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++
// 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 versa
bool canMake(int n, int ar[])
{
// Base Case
if (n == 1)
return false;
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 Code
int 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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
static boolean canMake(int n, int ar[])
{
// Base Case
if (n == 1)
return false;
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 Code
public 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
# Python3 program for the above approach
# Function to check if the given array
# forms an increasing decreasing
# sequence or vice versa
def canMake(n, ar):
# Base Case
if (n == 1):
return False;
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 arr
arr = [1, 2, 3, 4, 5];
n = len(arr);
# Function Call
if (canMake(n, arr)==False):
print("Yes");
else:
print("No");
# This code is contributed by PrinciRaj1992
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
static bool canMake(int n, int []ar)
{
// Base Case
if (n == 1)
return false;
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 Code
public 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
JavaScript
<script>
// Javascript program for the above approach
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
function canMake(n, ar)
{
// Base Case
if (n == 1)
return false;
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 Call
if (!canMake(n, arr))
{
document.write("Yes");
}
else
{
document.write("No");
}
// This code is contributed by sravan kumar
</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;
// Function to check if array can be divided into increasing and decreasing sub-arrays
string Divide_Array(int arr[], int N) {
bool increasing = false; // Flag to track increasing sub-array
bool decreasing = false; // Flag to track decreasing sub-array
// traverse through the array
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 both increasing and decreasing flags are true, array can be divided
if (increasing && decreasing) {
return "Yes";
}
}
// If both increasing and decreasing flags are not true, array cannot be divided
return "No";
}
int main() {
// Given array arr[]
int arr[] = { 1, 2, 3, 4, 5 };
int N= sizeof arr / sizeof arr[0];
// Function Call
string result = Divide_Array(arr, N);
cout << result << endl;
return 0;
}
Java
import java.util.*;
public class Main {
// Function to check if array can be divided into increasing and decreasing sub-arrays
static String divideArray(int[] arr, int N) {
boolean increasing = false; // Flag to track increasing sub-array
boolean decreasing = false; // Flag to track decreasing sub-array
// Traverse through the array
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 both increasing and decreasing flags are true, array can be divided
if (increasing && decreasing) {
return "Yes";
}
}
// If both increasing and decreasing flags are not true, array cannot be divided
return "No";
}
// Driver Code
public static void main(String[] args) {
// Given array arr[]
int[] arr = { 1, 2, 3, 4, 5 };
int N = arr.length;
String result = divideArray(arr, N);
System.out.println(result);
}
}
Python3
# Function to check if array can be divided into increasing and decreasing sub-arrays
def divide_array(arr):
increasing = False # Flag to track increasing sub-array
decreasing = False # Flag to track decreasing sub-array
# Traverse through the array
for i in range(1, len(arr)):
if arr[i] > arr[i - 1]:
increasing = True
elif arr[i] < arr[i - 1]:
decreasing = True
# If both increasing and decreasing flags are true, array can be divided
if increasing and decreasing:
return "Yes"
# If both increasing and decreasing flags are not true, array cannot be divided
return "No"
# Driver code
if __name__ == "__main__":
# Given array arr[]
arr = [1, 2, 3, 4, 5]
# Function Call
result = divide_array(arr)
print(result)
C#
using System;
class Program {
// Function to check if the array can be divided into
// increasing and decreasing sub-arrays
static string DivideArray(int[] arr, int N)
{
bool increasing
= false; // Flag to track increasing sub-array
bool decreasing
= false; // Flag to track decreasing sub-array
// Traverse through the array
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 both increasing and decreasing flags are
// true, the array can be divided
if (increasing && decreasing) {
return "Yes";
}
}
// If both increasing and decreasing flags are not
// true, the array cannot be divided
return "No";
}
static void Main()
{
// Given array arr[]
int[] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
// Function Call
string result = DivideArray(arr, N);
Console.WriteLine(result);
Console.ReadKey();
}
}
JavaScript
// Function to check if an array can be divided into increasing and decreasing sub-arrays
function divideArray(arr) {
let increasing = false; // Flag to track increasing sub-array
let decreasing = false; // Flag to track decreasing sub-array
// Traverse through the array
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 both increasing and decreasing flags are true, the array can be divided
if (increasing && decreasing) {
return "Yes";
}
}
// If both increasing and decreasing flags are not true, the array cannot be divided
return "No";
}
// Main function
function main() {
// Given array arr[]
const arr = [1, 2, 3, 4, 5];
// Function Call
const result = divideArray(arr);
console.log(result);
}
// Call the main function to execute the code
main();
// This code is contributed by shivamgupta310570
Time Complexity: O(N), where N is the size of an array
Auxiliary Space: O(1), as we are not using any extra space .
Similar Reads
Check if array can be converted into strictly decreasing sequence
Given an array arr[], the task is to check whether given array can be converted into a strictly decreasing sequence with the help of the below operation: Decrease any element(if it is greater than 1) by 1 in one operation Examples: Input: arr[] = {11, 11, 11, 11} Output : Yes Explanation: The given
5 min read
C++ Program to 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
4 min read
Maximum contiguous decreasing sequence obtained by removing any one element
Given an array arr[] of N integers. The task is to find the length of the contiguous strictly decreasing sequence that can be derived after removing at most one element from the array arr[]. Examples Input: arr[] = {8, 7, 3, 5, 2, 9} Output: 4 Explanation: If we remove 3, The maximum length of decre
10 min read
Generate an alternate increasing and decreasing Array
Given a string str of size N containing two types of character only that are "I" or "D". The task is to generate an array arr[0, 1, . . N] of size N + 1 satisfying the following conditions: If str[i] == "I" then arr[i] < arr[i+1]If str[i] == "D" then arr[i] > arr[i+1] Examples: Input: str = "I
5 min read
Sort array such that absolute difference of adjacent elements is in increasing order
Given an unsorted array of length N. The task is to sort the array, such that abs(a[i]-a[i+1]) < = abs(a[i+1]-a[i+2]) for all 0 < = i< N that is abs(a[0]-a[1]) < = abs(a[1]-a[2]) < = abs(a[2]-a[3]) and so on.Examples: Input: arr[] = {7, 4, 9, 9, -1, 9}Output: {9, 7, 9, 4, 9, -1}Explan
7 min read
Count of non-decreasing Arrays arr3[] such that arr1[i] <= arr3[i] <= arr2[i]
Given two arrays arr1[] and arr2[] having N integers in non-decreasing order, the task is to find the count of non-decreasing arrays arr3[] of length N such that arr1[i] <= arr3[i] <= arr2[i] for all values of i in range [0, N). Examples: Input: arr1[] = {1, 1}, arr2[] = {2, 3}Output: 5Explana
6 min read
Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
Given an array arr[] of non-negative integers. We have to delete elements from this array such that arr[i + 1] > arr[j] for each valid i and this will be counted as one step. We have to apply the same operations until the array has become strictly decreasing. Now the task is to count the number o
6 min read
C++ Program to Print all triplets in sorted array that form AP
Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples :Â Â Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Input : arr[] = { 3, 5, 6, 7, 8, 10, 12}; Ou
4 min read
C++ Program to Check if it is possible to sort the array after rotating it
Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
3 min read
Maximum length sub-array which satisfies the given conditions
Given an array arr[] of N integers, the task is to find the maximum length of any sub-array of arr[] which satisfies one of the given conditions: The subarray is strictly increasing.The subarray is strictly decreasing.The subarray is first strictly increasing then strictly decreasing. Examples: Inpu
9 min read