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:

  1. The subarray is strictly increasing.
  2. The subarray is strictly decreasing.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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] *
    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] *
    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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.