Check if sequence of removed middle elements from an array is sorted or not | Function to check if sequence of removed middle elements from an array is sorted or not ; Points toa the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
function isSortedArray ( arr , n ) { var l = 0 var r = ( n - 1 ) while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l += 1 r -= 1 } else return false } return true } var arr = [ 4 , 3 , 1 , 2 , 5 ] var N = arr . length if ( isSortedArray ( arr , N ) ) console . log ( " " ) else console . log ( " " )