Find if a crest is present in the index range [ L , R ] of the given array | Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code
function hasCrest ( arr , n , L , R ) { let present = new Uint8Array ( n ) ; for ( let i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( let i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } let arr = [ 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 ] ; let N = arr . length ; let L = 2 ; let R = 6 ; if ( hasCrest ( arr , N , L , R ) ) document . write ( " " ) ; else document . write ( " " ) ;