Maximum length of Strictly Increasing Sub-array after removing at most one element
Last Updated :
09 Dec, 2022
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:
- Create two arrays pre[] and pos[] of size N.
- Iterate over the input array arr[] from (0, N) to find out the contribution of the current element arr[i] in the array till now [0, i) and update the pre[] array if it contributes to the strictly increasing subarray.
- Iterate over the input array arr[] from [N - 2, 0] to find out the contribution of the current element arr[j] in the array till now (N, j) and update the pos[] array if arr[j] contributes in the longest increasing subarray.
- Calculate the maximum length of the strictly increasing subarray without removing any element.
- Iterate over the array pre[] and pos[] to find out the contribution of the current element by excluding that element.
- Maintain a variable ans to find the maximum found till now.
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 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 strictly
// 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;
}
C
// C implementation of the approach
#include <stdio.h>
// Find maximum between two numbers.
int max(int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
// 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];
int pos[n];
for (int i = 0; i < n; i++)
pre[i] = 0;
for (int i = 0; i < n; i++)
pos[i] = 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 strictly
// 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);
printf("%d", maxIncSubarr(arr, n));
return 0;
}
// This code is contributed by Sania Kumari Gupta
Java
// Java implementation of the approach
import java.io.*;
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
// strictly 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
Python3
# 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
# strictly 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 - 1):
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
C#
// 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
// strictly 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
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the maximum length of
// strictly increasing subarray after
// removing atmost one element
function maxIncSubarr(a, n)
{
// Create two arrays pre and pos
let pre = new Array(n);
let pos = new Array(n);
pre.fill(0);
pos.fill(0);
pre[0] = 1;
pos[n - 1] = 1;
let l = 0;
// Find out the contribution of the current
// element in array[0, i] and update pre[i]
for(let 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(let 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
// strictly increasing subarray without
// removing any element
let ans = 0;
l = 1;
for(let 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(let 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
let arr = [ 1, 2 ];
let n = arr.length;
document.write(maxIncSubarr(arr, n));
// This code is contributed by rameshtravel07
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Maximum subarray sum possible after removing at most K array elements Given an array arr[] of size N and an integer K, the task is to find the maximum subarray sum by removing at most K elements from the array. Examples: Input: arr[] = { -2, 1, 3, -2, 4, -7, 20 }, K = 1 Output: 26 Explanation: Removing arr[5] from the array modifies arr[] to { -2, 1, 3, -2, 4, 20 } Su
15+ min read
Check whether an array can be made strictly increasing by removing at most one element Given an array arr[] consisting of N integers, the task is to check whether it is possible to make the given array strictly increasing by removing at most one element. If it is possible to make the array strictly increasing, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {1, 1, 2}
8 min read
Maximize length of Non-Decreasing Subsequence by reversing at most one Subarray Given a binary array arr[], the task is to find the maximum possible length of non-decreasing subsequence that can be generated by reversing a subarray at most once. Examples: Input: arr[] = {0, 1, 0, 1} Output: 4 Explanation: After reversing the subarray from index [2, 3], the array modifies to {0,
9 min read
Maximize the maximum subarray sum after removing atmost one element Given an array arr[] of N integers, the task is to find the maximum sum of a subarray with at most one deletion allowed. The size of subarray to be considered should be at least 1.Examples: Input: arr[] = {1, 2, 3, -2, 3} Output: 9 The maximum sub-array sum is given by the sub-array {2, 3, -2, 3} He
2 min read
Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements Given a binary array arr[] consisting of N elements, the task is to find the maximum possible length of a subarray of only 1âs, after deleting a single pair of consecutive array elements. If no such subarray exists, print -1. Examples: Input: arr[] = {1, 1, 1, 0, 0, 1} Output: 4 Explanation: Removal
15+ min read
Queries to find the maximum array element after removing elements from a given range Given an array arr[] and an array Q[][] consisting of queries of the form of {L, R}, the task for each query is to find the maximum array element after removing array elements from the range of indices [L, R]. If the array becomes empty after removing the elements from given range of indices, then p
10 min read