Number of segments where all elements are greater than X | Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code
function countSegments ( a , n , x ) { let flag = false ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } let a = [ 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ] ; let n = a . length ; let x = 13 ; document . write ( countSegments ( a , n , x ) ) ;