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
< ? php function countSegments ( $ a , $ n , $ x ) { $ flag = false ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ x ) { $ flag = true ; } else { if ( $ flag ) $ count += 1 ; $ flag = false ; } } if ( $ flag ) $ count += 1 ; return $ count ; } $ a = array ( 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ) ; $ n = sizeof ( $ a ) ; $ x = 13 ; echo countSegments ( $ a , $ n , $ x ) ; ? >