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}
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 ?> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Minimum number of segments required such that each segment has distinct elements
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Number of positions such that adding K to the element is greater than sum of all other elements
- Find the number of elements greater than k in a sorted array
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Count the number of elements which are greater than any of element on right side of an array
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count elements such that there are exactly X elements with values greater than or equal to X
- Find all elements in array which have at-least two greater elements
- Maximum number of segments that can contain the given points
- Maximize the number of segments of length p, q and r
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Split the array into odd number of segments of odd lengths
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.



