Home > Backend Development > C++ > Written in C++, find the number of segments where all elements are greater than X

Written in C++, find the number of segments where all elements are greater than X

王林
Release: 2023-09-10 09:29:13
forward
637 people have browsed it

Written in C++, find the number of segments where all elements are greater than X

In this article, we need to find the number of segments or sub-arrays in a given sequence that are greater than a given number X.

We can only count overlapping segments once, two adjacent elements or segments should not be counted separately. So here is a 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
Copy after login

Ways to find the solution

Naive way

In this problem we initialize the variables with 0state and start processing the given array, when an element greater than X is found, change the state to 1 and continue processing elements; when a number less than or equal to X is found, change the state back to 0, each time When the status changes to 1 and returns, count is increased by 1 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;
}
Copy after login

Output

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

Description of the above program

In the above program, we use the state as a switch when a number greater than X is found Set it to 1, when we find a number greater than X we set it to 0 when we find a number less than or equal to Finally, print the results stored in the count.

Conclusion h2>

In this article, we solved the problem of finding the number of segments where all elements are greater than X by applying a method 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.

The above is the detailed content of Written in C++, find the number of segments where all elements are greater than X. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template