Number of segments where all elements are greater than X | C ++ program to count the number of segments in which 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSegments ( int a [ ] , int n , int x ) { bool flag = false ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } int main ( ) { int a [ ] = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int x = 13 ; cout << countSegments ( a , n , x ) ; return 0 ; }