Maximum length of Strictly Increasing Sub | Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
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 ) ) ;