Maximum length sub-array which satisfies the given conditions
Last Updated :
28 Sep, 2022
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:
Input: arr[] = {1, 2, 2, 1, 3}
Output: 2
{1, 2}, {2, 1} and {1, 3} are the valid subarrays.
Input: arr[] = {5, 4, 3, 2, 1, 2, 3, 4}
Output: 5
{5, 4, 3, 2, 1} is the required subarray.
Approach: Create an array incEnding[] where incEnding[i] will store the length of the largest increasing subarray of the given array ending at index i. Similarly, create another array decStarting[] where decStarting[i] will store the length of the largest decreasing subarray of the given array starting at the index i. Now start traversing the original array and for every element, assume it to be the mid of the required subarray then the length of the largest required subarray whose mid at index i will be incEnding[i] + decStarting[i] - 1. Note that 1 is subtracted because arr[i] will be counted twice for both the increasing and the decreasing subarray.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the largest
// required sub-array
int largestSubArr(int arr[], int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int incEnding[n] = { 0 };
incEnding[0] = 1;
for (int i = 1; i < n; i++) {
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int decStarting[n] = { 0 };
decStarting[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (int i = 0; i < n; i++) {
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = max(maxSubArr, incEnding[i]
+ decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 2, 1, 3 };
int n = sizeof(arr) / sizeof(int);
cout << largestSubArr(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the largest
// required sub-array
static int largestSubArr(int arr[], int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int incEnding[] = new int[n];
int i;
for(i = 0; i < n ; i++)
incEnding[i] = 0;
incEnding[0] = 1;
for (i = 1; i < n; i++)
{
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int decStarting[] = new int[n];
for(i = 0; i < n ; i++)
decStarting[i] = 0;
decStarting[n - 1] = 1;
for (i = n - 2; i >= 0; i--)
{
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (i = 0; i < n; i++)
{
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = Math.max(maxSubArr, incEnding[i] +
decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 2, 1, 3 };
int n = arr.length;
System.out.println(largestSubArr(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the largest
# required sub-array
def largestSubArr(arr, n) :
# incEnding[i] will store the length
# of the largest increasing subarray
# ending at arr[i]
incEnding = [0] * n
incEnding[0] = 1
for i in range(1, n) :
# If current element is greater than
# the previous element then it
# can be a part of the previous
# increasing subarray
if (arr[i - 1] < arr[i]) :
incEnding[i] = incEnding[i - 1] + 1
else :
incEnding[i] = 1
# decStarting[i] will store the length
# of the largest decreasing subarray
# starting at arr[i]
decStarting = [0] * n
decStarting[n - 1] = 1
for i in range(n - 2, -1, -1):
# If current element is greater than
# the next element then it can be a part
# of the decreasing subarray
# with the next element
if (arr[i + 1] < arr[i]) :
decStarting[i] = decStarting[i + 1] + 1
else :
decStarting[i] = 1
# To store the length of the
# maximum required subarray
maxSubArr = 0
# Assume every element to be the mid
# point of the required array
for i in range(n):
# 1 has to be subtracted because the
# current element will be counted for
# both the increasing and
# the decreasing subarray
maxSubArr = max(maxSubArr, incEnding[i] +
decStarting[i] - 1)
return maxSubArr
# Driver code
arr = [ 1, 2, 2, 1, 3 ]
n = len(arr)
print(largestSubArr(arr, n))
# This code is contributed by
# divyamohan123
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the largest
// required sub-array
static int largestSubArr(int []arr, int n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
int []incEnding = new int[n];
int i;
for(i = 0; i < n ; i++)
incEnding[i] = 0;
incEnding[0] = 1;
for (i = 1; i < n; i++)
{
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
int []decStarting = new int[n];
for(i = 0; i < n ; i++)
decStarting[i] = 0;
decStarting[n - 1] = 1;
for (i = n - 2; i >= 0; i--)
{
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
int maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for (i = 0; i < n; i++)
{
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = Math.Max(maxSubArr, incEnding[i] +
decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 1, 2, 2, 1, 3 };
int n = arr.Length;
Console.WriteLine(largestSubArr(arr, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the largest
// required sub-array
function largestSubArr(arr, n)
{
// incEnding[i] will store the length
// of the largest increasing subarray
// ending at arr[i]
var incEnding = Array(n).fill(0);
incEnding[0] = 1;
for(var i = 1; i < n; i++)
{
// If current element is greater than
// the previous element then it
// can be a part of the previous
// increasing subarray
if (arr[i - 1] < arr[i])
incEnding[i] = incEnding[i - 1] + 1;
else
incEnding[i] = 1;
}
// decStarting[i] will store the length
// of the largest decreasing subarray
// starting at arr[i]
var decStarting = Array(n).fill(0);
decStarting[n - 1] = 1;
for(var i = n - 2; i >= 0; i--)
{
// If current element is greater than
// the next element then it can be a part
// of the decreasing subarray
// with the next element
if (arr[i + 1] < arr[i])
decStarting[i] = decStarting[i + 1] + 1;
else
decStarting[i] = 1;
}
// To store the length of the
// maximum required subarray
var maxSubArr = 0;
// Assume every element to be the mid
// point of the required array
for(var i = 0; i < n; i++)
{
// 1 has to be subtracted because the
// current element will be counted for
// both the increasing and
// the decreasing subarray
maxSubArr = Math.max(maxSubArr,
incEnding[i] +
decStarting[i] - 1);
}
return maxSubArr;
}
// Driver code
var arr = [ 1, 2, 2, 1, 3 ];
var n = arr.length;
document.write(largestSubArr(arr, n));
// This code is contributed by itsok
</script>
Time complexity: O(n) where n is the size of the given array
Auxiliary space: O(n) because using extra space for arrays incEnding and decStarting
Similar Reads
Find maximum Subsequence Sum according to given conditions Given an integer array nums and an integer K, The task is to find the maximum sum of a non-empty subsequence of the array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= K is satisfied. A subsequence of an array is obtai
5 min read
Longest unique subarray of an Array with maximum sum in another Array Given two arrays X[] and Y[] of size N, the task is to find the longest subarray in X[] containing only unique values such that a subarray with similar indices in Y[] should have a maximum sum. The value of array elements is in the range [0, 1000]. Examples: Input: N = 5, X[] = {0, 1, 2, 0, 2}, Y[]
10 min read
Length of longest subarray for each index in Array where element at that index is largest Given an array arr[] of size N, the task is to calculate, for i(0<=i<N), the maximum length of a subarray containing arr[i], where arr[i] is the maximum element. Example: Input : arr[ ] = {62, 97, 49, 59, 54, 92, 21}, N=7Output: 1 7 1 3 1 5 1Explanation: The maximum length of subarray in which
15 min read
Maximum Sum SubArray using Divide and Conquer | Set 2 Given an array arr[] of integers, the task is to find the maximum sum sub-array among all the possible sub-arrays.Examples: Input: arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4} Output: 6 {4, -1, 2, 1} is the required sub-array.Input: arr[] = {2, 2, -2} Output: 4 Approach: Till now we are only aware of Kad
13 min read
Sum of Max of Subarrays Given an array arr[], the task is to find the sum of the maximum elements of every possible non-empty sub-arrays of the given array arr[].Examples: Input: arr[] = [1, 3, 2]Output: 15Explanation: All possible non-empty subarrays of [1, 3, 2] are {1}, {3}, {2}, {1, 3}, {3, 2} and {1, 3, 2}. The maximu
12 min read