Maximum length of Strictly Increasing Sub-array after removing at most one element

Given an array arr[], the task is to remove at most one element and calculate the maximum length of strictly increasing subarray.

Examples:

Input: arr[] = {1, 2, 5, 3, 4}
Output: 4
After deleting 5, the resulting array will be {1, 2, 3, 4}
and the maximum length of its strictly increasing subarray is 4.

Input: arr[] = {1, 2}
Output: 2
The complete array is already strictly increasing.

Approach:



Below is the implementation of the above approach:

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 maximum length of
// strictly increasing subarray after
// removing atmost one element
int maxIncSubarr(int a[], int n)
{
    // Create two arrays pre and pos
    int pre[n] = { 0 };
    int pos[n] = { 0 };
    pre[0] = 1;
    pos[n - 1] = 1;
    int l = 0;
  
    // Find out the contribution of the current
    // element in array[0, i] and update pre[i]
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1])
            pre[i] = pre[i - 1] + 1;
        else
            pre[i] = 1;
    }
  
    // Find out the contribution of the current
    // element in array[N - 1, i] and update pos[i]
    l = 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] < a[i + 1])
            pos[i] = pos[i + 1] + 1;
        else
            pos[i] = 1;
    }
  
    // Calculate the maximum length of the
    // stricly increasing subarray without
    // removing any element
    int ans = 0;
    l = 1;
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1])
            l++;
        else
            l = 1;
        ans = max(ans, l);
    }
  
    // Calculate the maximum length of the
    // strictly increasing subarray after
    // removing the current element
    for (int i = 1; i <= n - 2; i++) {
        if (a[i - 1] < a[i + 1])
            ans = max(pre[i - 1] + pos[i + 1], ans);
    }
  
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxIncSubarr(arr, n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to return the maximum length of 
    // strictly increasing subarray after 
    // removing atmost one element 
    static int maxIncSubarr(int a[], int n) 
    
        // Create two arrays pre and pos 
        int pre[] = new int[n] ; 
        int pos[] = new int[n] ; 
        pre[0] = 1
        pos[n - 1] = 1
        int l = 0
      
        // Find out the contribution of the current 
        // element in array[0, i] and update pre[i] 
        for (int i = 1; i < n; i++)
        
            if (a[i] > a[i - 1]) 
                pre[i] = pre[i - 1] + 1
            else
                pre[i] = 1
        
      
        // Find out the contribution of the current 
        // element in array[N - 1, i] and update pos[i] 
        l = 1
        for (int i = n - 2; i >= 0; i--)
        
            if (a[i] < a[i + 1]) 
                pos[i] = pos[i + 1] + 1
            else
                pos[i] = 1
        
      
        // Calculate the maximum length of the 
        // stricly increasing subarray without 
        // removing any element 
        int ans = 0
        l = 1
        for (int i = 1; i < n; i++)
        
            if (a[i] > a[i - 1]) 
                l++; 
            else
                l = 1
            ans = Math.max(ans, l); 
        
      
        // Calculate the maximum length of the 
        // strictly increasing subarray after 
        // removing the current element 
        for (int i = 1; i <= n - 2; i++) 
        
            if (a[i - 1] < a[i + 1]) 
                ans = Math.max(pre[i - 1] + 
                                pos[i + 1], ans); 
        
        return ans; 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int arr[] = {1, 2}; 
        int n = arr.length; 
      
        System.out.println(maxIncSubarr(arr, n)); 
    
}
  
// This code is contributed by AnkitRai01
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach 
  
# Function to return the maximum length of
# strictly increasing subarray after
# removing atmost one element
def maxIncSubarr(a, n):
      
    # Create two arrays pre and pos
    pre = [0] * n;
    pos = [0] * n;
    pre[0] = 1;
    pos[n - 1] = 1;
    l = 0;
  
    # Find out the contribution of the current
    # element in array[0, i] and update pre[i]
    for i in range(1, n):
        if (a[i] > a[i - 1]):
            pre[i] = pre[i - 1] + 1;
        else:
            pre[i] = 1;
      
    # Find out the contribution of the current
    # element in array[N - 1, i] and update pos[i]
    l = 1;
    for i in range(n - 2, -1, -1):
        if (a[i] < a[i + 1]):
            pos[i] = pos[i + 1] + 1;
        else:
            pos[i] = 1;
      
    # Calculate the maximum length of the
    # stricly increasing subarray without
    # removing any element
    ans = 0;
    l = 1;
    for i in range(1, n):
        if (a[i] > a[i - 1]):
            l += 1;
        else:
            l = 1;
        ans = max(ans, l);
      
    # Calculate the maximum length of the
    # strictly increasing subarray after
    # removing the current element
    for i in range(1, n - 2):
        if (a[i - 1] < a[i + 1]):
            ans = max(pre[i - 1] + pos[i + 1], ans);
      
    return ans;
  
# Driver code
if __name__ == '__main__':
    arr = [ 1, 2 ];
    n = len(arr);
  
    print(maxIncSubarr(arr, n));
      
# This code is contributed by PrinciRaj1992
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
{
      
    // Function to return the maximum length of 
    // strictly increasing subarray after 
    // removing atmost one element 
    static int maxIncSubarr(int []a, int n) 
    
        // Create two arrays pre and pos 
        int []pre = new int[n] ; 
        int []pos = new int[n] ; 
        pre[0] = 1; 
        pos[n - 1] = 1; 
        int l = 0; 
      
        // Find out the contribution of the current 
        // element in array[0, i] and update pre[i] 
        for (int i = 1; i < n; i++)
        
            if (a[i] > a[i - 1]) 
                pre[i] = pre[i - 1] + 1; 
            else
                pre[i] = 1; 
        
      
        // Find out the contribution of the current 
        // element in array[N - 1, i] and update pos[i] 
        l = 1; 
        for (int i = n - 2; i >= 0; i--)
        
            if (a[i] < a[i + 1]) 
                pos[i] = pos[i + 1] + 1; 
            else
                pos[i] = 1; 
        
      
        // Calculate the maximum length of the 
        // stricly increasing subarray without 
        // removing any element 
        int ans = 0; 
        l = 1; 
        for (int i = 1; i < n; i++)
        
            if (a[i] > a[i - 1]) 
                l++; 
            else
                l = 1; 
            ans = Math.Max(ans, l); 
        
      
        // Calculate the maximum length of the 
        // strictly increasing subarray after 
        // removing the current element 
        for (int i = 1; i <= n - 2; i++) 
        
            if (a[i - 1] < a[i + 1]) 
                ans = Math.Max(pre[i - 1] + 
                                pos[i + 1], ans); 
        
        return ans; 
    
      
    // Driver code 
    public static void Main()
    
        int []arr = {1, 2}; 
        int n = arr.Length; 
      
        Console.WriteLine(maxIncSubarr(arr, n)); 
    
}
  
// This code is contributed by AnkitRai01
chevron_right

Output:
2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





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 :