# Number of segments where all elements are greater than X

Given an array of N integers and a number X, the task is to find the number of segments such that all elements in the segment are greater than X. The count should not include overlapping segments and two or more contiguous segments should be considered as one.

Examples:

Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, X = 5
Output: 2
The two segments are {6, 7} and {10, 11}.

Input: a[] = {8, 25, 10, 19, 19, 18, 20, 11, 18}, X = 13
Output: 3
The segments are {25}, {19, 19, 18, 20} and {18}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize a flag counter as false, mark it true whenever any element greater than X occurs. Whenever any element is not greater than X, count the segment if the flag is true. Re-initialize flag to false whenever any element <=X appears.

Below is the implementation of the above approach.

## C++

 `// C++ program to count the number of segments ` `// in which all elements are greater than X ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count number of segments ` `int` `countSegments(``int` `a[], ``int` `n, ``int` `x) ` `{ ` `    ``bool` `flag = ``false``; ` ` `  `    ``int` `count = 0; ` ` `  `    ``// Iterate in the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// check if array element ` `        ``// greater then X or not ` `        ``if` `(a[i] > x) { ` `            ``flag = ``true``; ` `        ``} ` `        ``else` `{ ` ` `  `            ``// if flag is true ` `            ``if` `(flag) ` `                ``count += 1; ` ` `  `            ``flag = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// After iteration complete ` `    ``// check for the last segment ` `    ``if` `(flag) ` `        ``count += 1; ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `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; ` `} `

## Java

 `// Java program to count the number of segments ` `// in which all elements are greater than X ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  ` `  `// Function to count number of segments ` `static` `int` `countSegments(``int` `a[], ``int` `n, ``int` `x) ` `{ ` `    ``boolean` `flag = ``false``; ` ` `  `    ``int` `count = ``0``; ` ` `  `    ``// Iterate in the array ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `        ``// check if array element ` `        ``// greater then X or not ` `        ``if` `(a[i] > x) { ` `            ``flag = ``true``; ` `        ``} ` `        ``else` `{ ` ` `  `            ``// if flag is true ` `            ``if` `(flag) ` `                ``count += ``1``; ` ` `  `            ``flag = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// After iteration complete ` `    ``// check for the last segment ` `    ``if` `(flag) ` `        ``count += ``1``; ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `        ``int` `a[] = { ``8``, ``25``, ``10``, ``19``, ``19``, ``18``, ``20``, ``11``, ``18` `}; ` `    ``int` `n =a.length; ` `    ``int` `x = ``13``; ` `    ``System.out.println(countSegments(a, n, x)); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python 3 program to count the number of segments ` `# in which all elements are greater than X ` ` `  `# Function to count number of segments ` `def` `countSegments(a, n, x): ` `    ``flag ``=` `False` ` `  `    ``count ``=` `0` ` `  `    ``# Iterate in the array ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# check if array element greater  ` `        ``# then X or not ` `        ``if` `(a[i] > x): ` `            ``flag ``=` `True` `         `  `        ``else``: ` `             `  `            ``# if flag is true ` `            ``if` `(flag): ` `                ``count ``+``=` `1` ` `  `            ``flag ``=` `False` `     `  `    ``# After iteration complete ` `    ``# check for the last segment ` `    ``if` `(flag): ` `        ``count ``+``=` `1` ` `  `    ``return` `count ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `[``8``, ``25``, ``10``, ``19``, ``19``, ``18``, ``20``, ``11``, ``18``] ` `    ``n ``=` `len``(a) ` `    ``x ``=` `13` `    ``print``(countSegments(a, n, x)) ` ` `  `# This code is contributed by ` `# Sanjit_Prasad `

## C#

 `// C# program to count the number of segments  ` `// in which all elements are greater than X  ` `using` `System; ` ` `  `public` `class` `GFG{ ` `     `  `// Function to count number of segments  ` `static` `int` `countSegments(``int` `[]a, ``int` `n, ``int` `x)  ` `{  ` `    ``bool` `flag = ``false``;  ` ` `  `    ``int` `count = 0;  ` ` `  `    ``// Iterate in the array  ` `    ``for` `(``int` `i = 0; i < n; i++) {  ` ` `  `        ``// check if array element  ` `        ``// greater then X or not  ` `        ``if` `(a[i] > x) {  ` `            ``flag = ``true``;  ` `        ``}  ` `        ``else` `{  ` ` `  `            ``// if flag is true  ` `            ``if` `(flag)  ` `                ``count += 1;  ` ` `  `            ``flag = ``false``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// After iteration complete  ` `    ``// check for the last segment  ` `    ``if` `(flag)  ` `        ``count += 1;  ` ` `  `    ``return` `count;  ` `}  ` ` `  `    ``static` `public` `void` `Main (){ ` `     `  `    ``int` `[]a = { 8, 25, 10, 19, 19, 18, 20, 11, 18 };  ` `    ``int` `n =a.Length;  ` `    ``int` `x = 13;  ` `    ``Console.WriteLine(countSegments(a, n, x));  ` `    ``}  ` `}  `

## PHP

 ` ``\$x``)  ` `        ``{  ` `            ``\$flag` `= true;  ` `        ``}  ` `        ``else`  `        ``{  ` ` `  `            ``// if flag is true  ` `            ``if` `(``\$flag``)  ` `                ``\$count` `+= 1;  ` ` `  `            ``\$flag` `= false;  ` `        ``}  ` `    ``}  ` ` `  `    ``// After iteration complete  ` `    ``// check for the last segment  ` `    ``if` `(``\$flag``)  ` `        ``\$count` `+= 1;  ` ` `  `    ``return` `\$count``;  ` `}  ` ` `  `// Driver Code  ` `\$a` `= ``array``( 8, 25, 10, 19, 19,  ` `            ``18, 20, 11, 18 );  ` `\$n` `= sizeof(``\$a``);  ` `\$x` `= 13;  ` `echo` `countSegments(``\$a``, ``\$n``, ``\$x``);  ` ` `  `// This code is contributed by ajit ` `?> `

Output:

```3
```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, jit_t, Sanjit_Prasad