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 incresing 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 endning 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 |
2
Recommended Posts:
- Maximum length sub-array which satisfies the given conditions
- Check if the Matrix satisfies the given conditions
- Longest sub-sequence that satisfies the given conditions
- Generate an array of size K which satisfies the given conditions
- Count of operations required to update the array such that it satisfies the given conditions
- Maximum length of subarray such that sum of the subarray is even
- Find maximum average subarray of k length
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum size of sub-array that satisfies the given condition
- Find the length of largest subarray with 0 sum
- Length of the longest alternating subarray
- Length of the longest Subarray with only Even Elements
- Minimum length subarray of 1s in a Binary Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



