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 = 5Output: 2

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

The segments are {25}, {19, 19, 18, 20} and {18}

**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 <bits/stdc++.h>` `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

`<?php` `// PHP program to count the number` `// of segments in which all elements` `// are greater than X` `// Function to count number of segments` `function` `countSegments(` `$a` `, ` `$n` `, ` `$x` `)` `{` ` ` `$flag` `= false;` ` ` `$count` `= 0;` ` ` `// Iterate in the array` ` ` `for` `(` `$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` `$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` `?>` |

## Javascript

`<script>` ` ` `// Javascript program to count` ` ` `// the number of segments` ` ` `// in which all elements` ` ` `// are greater than X` ` ` ` ` `// Function to count number of segments` ` ` `function` `countSegments(a, n, x)` ` ` `{` ` ` `let flag = ` `false` `;` ` ` `let count = 0;` ` ` `// Iterate in the array` ` ` `for` `(let 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;` ` ` `}` ` ` `let a = [ 8, 25, 10, 19, 19, 18, 20, 11, 18 ];` ` ` `let n = a.length;` ` ` `let x = 13;` ` ` `document.write(countSegments(a, n, x));` ` ` `</script>` |

**Output:**

3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live** and **Geeks Classes Live USA**