Maximum size of sub | Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Prvar the required answer
function cmp ( a , b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } function maxSubarraySize ( arr , n ) { var ans = 1 ; var anchor = 0 ; for ( i = 1 ; i < n ; i ++ ) { var c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } var arr = [ 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ] ; var n = arr . length ; document . write ( maxSubarraySize ( arr , n ) ) ;