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++
#include <bits/stdc++.h>
using namespace std;
int maxIncSubarr( int a[], int n)
{
int pre[n] = { 0 };
int pos[n] = { 0 };
pre[0] = 1;
pos[n - 1] = 1;
int l = 0;
for ( int i = 1; i < n; i++) {
if (a[i] > a[i - 1])
pre[i] = pre[i - 1] + 1;
else
pre[i] = 1;
}
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;
}
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);
}
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;
}
int main()
{
int arr[] = { 1, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << maxIncSubarr(arr, n);
return 0;
}
|
C
#include <stdio.h>
int max( int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
int maxIncSubarr( int a[], int n)
{
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;
for ( int i = 1; i < n; i++) {
if (a[i] > a[i - 1])
pre[i] = pre[i - 1] + 1;
else
pre[i] = 1;
}
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;
}
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);
}
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;
}
int main()
{
int arr[] = { 1, 2 };
int n = sizeof (arr) / sizeof ( int );
printf ( "%d" , maxIncSubarr(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maxIncSubarr( int a[], int n)
{
int pre[] = new int [n] ;
int pos[] = new int [n] ;
pre[ 0 ] = 1 ;
pos[n - 1 ] = 1 ;
int l = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] > a[i - 1 ])
pre[i] = pre[i - 1 ] + 1 ;
else
pre[i] = 1 ;
}
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 ;
}
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);
}
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;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 };
int n = arr.length;
System.out.println(maxIncSubarr(arr, n));
}
}
|
Python3
def maxIncSubarr(a, n):
pre = [ 0 ] * n;
pos = [ 0 ] * n;
pre[ 0 ] = 1 ;
pos[n - 1 ] = 1 ;
l = 0 ;
for i in range ( 1 , n):
if (a[i] > a[i - 1 ]):
pre[i] = pre[i - 1 ] + 1 ;
else :
pre[i] = 1 ;
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 ;
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);
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;
if __name__ = = '__main__' :
arr = [ 1 , 2 ];
n = len (arr);
print (maxIncSubarr(arr, n));
|
C#
using System;
class GFG
{
static int maxIncSubarr( int []a, int n)
{
int []pre = new int [n] ;
int []pos = new int [n] ;
pre[0] = 1;
pos[n - 1] = 1;
int l = 0;
for ( int i = 1; i < n; i++)
{
if (a[i] > a[i - 1])
pre[i] = pre[i - 1] + 1;
else
pre[i] = 1;
}
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;
}
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);
}
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;
}
public static void Main()
{
int []arr = {1, 2};
int n = arr.Length;
Console.WriteLine(maxIncSubarr(arr, n));
}
}
|
Javascript
<script>
function maxIncSubarr(a, n)
{
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;
for (let i = 1; i < n; i++)
{
if (a[i] > a[i - 1])
pre[i] = pre[i - 1] + 1;
else
pre[i] = 1;
}
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;
}
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);
}
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;
}
let arr = [ 1, 2 ];
let n = arr.length;
document.write(maxIncSubarr(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!