Open In App

Maximum length sub-array which satisfies the given conditions

Improve
Improve
Like Article
Like
Save
Share
Report

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 increasing then strictly decreasing.

Examples:  

Input: arr[] = {1, 2, 2, 1, 3} 
Output:
{1, 2}, {2, 1} and {1, 3} are the valid subarrays.

Input: arr[] = {5, 4, 3, 2, 1, 2, 3, 4} 
Output:
{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>


Output: 

2

 

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



Last Updated : 28 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads