- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the Number of segments where all elements are greater than X using C++

In this article, we have to find the number of segments or subarrays in a given sequence where elements greater than the given number X.

We can count overlapping segments only once, and two contiguous elements or segments should not count separately. So here is the basic example of the given problem −

Input : arr[ ] = { 9, 6, 7, 11, 5, 7, 8, 10, 3}, X = 7 Output : 3 Explanation : { 9 }, { 11 } and { 8, 10 } are the segments greater than 7 Input : arr[ ] = { 9, 6, 12, 2, 11, 14, 8, 14 }, X = 8 Output : 4 Explanation : { 9 }, { 12 }, { 11, 14 } and { 14 } are the segments greater than 8

## Approach to Find the Solution

### Naive Approach

In this problem, we are initializing a variable **state** with 0 and begin processing the given array and change the state to 1 when an element greater than X is found and keep on processing the elements and change the state back to 0 when number less than or equal to X is found and increment count with 1 for every time state goes to 1 and back to 0.

## Example

#include <bits/stdc++.h> using namespace std; int main (){ int a[] = { 9, 6, 12, 2, 11, 14, 8, 14 }; int n = sizeof (a) / sizeof (a[0]); int X = 8; int state = 0; int count = 0; // traverse the array for (int i = 0; i < n; i++){ // checking whether element is greater than X if (a[i] > X){ state = 1; } else{ // if flag is true if (state) count += 1; state = 0; } } // checking for the last segment if (state) count += 1; cout << "Number of segments where all elements are greater than X: " << count; return 0; }

## Output

Number of segments where all elements are greater than X: 4

## Explanation of the Above Program

In the above program, we are using the state as a switch, and we set it to 1 when a number greater than X is found and set it to 0 when a number less than or equal to X is found, and for every time state goes to 1 and comes back to 0 we increment the count by 1. Finally, printing the result stored in the count.

## Conclusion

In this article, we solve the problem of finding the number of segments where all elements are greater than X by applying an approach of setting the state to 1 and 0 whenever a segment is found. We can write this program in any other programming language like C, Java, Python, etc.

- Related Articles
- Find the number of elements greater than k in a sorted array using C++
- Program to find number not greater than n where all digits are non-decreasing in python
- Delete all the nodes from the list that are greater than x in C++
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Python program to print Rows where all its Elementsâ€™ frequency is greater than K
- MongoDB query where all array items are greater than a specified condition?
- Count subarrays with all elements greater than K in C++
- Number of elements greater than modified mean in matrix in C++
- How to find the smallest number greater than x in Python?
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Count number of substrings with numeric value greater than X in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Program to find X for special array with X elements greater than or equal X in Python
- Program to filter all values which are greater than x in an array