Number of segments where all elements are greater than X
Last Updated :
19 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int countSegments( int a[], int n, int x)
{
bool flag = false ;
int count = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > x) {
flag = true ;
}
else {
if (flag)
count += 1;
flag = false ;
}
}
if (flag)
count += 1;
return count;
}
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
import java.io.*;
class GFG {
static int countSegments( int a[], int n, int x)
{
boolean flag = false ;
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] > x) {
flag = true ;
}
else {
if (flag)
count += 1 ;
flag = false ;
}
}
if (flag)
count += 1 ;
return count;
}
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));
}
}
|
Python3
def countSegments(a, n, x):
flag = False
count = 0
for i in range (n):
if (a[i] > x):
flag = True
else :
if (flag):
count + = 1
flag = False
if (flag):
count + = 1
return count
if __name__ = = '__main__' :
a = [ 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ]
n = len (a)
x = 13
print (countSegments(a, n, x))
|
C#
using System;
public class GFG{
static int countSegments( int []a, int n, int x)
{
bool flag = false ;
int count = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > x) {
flag = true ;
}
else {
if (flag)
count += 1;
flag = false ;
}
}
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));
}
}
|
Javascript
<script>
function countSegments(a, n, x)
{
let flag = false ;
let count = 0;
for (let i = 0; i < n; i++) {
if (a[i] > x)
{
flag = true ;
}
else {
if (flag)
count += 1;
flag = false ;
}
}
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>
|
PHP
<?php
function countSegments( $a , $n , $x )
{
$flag = false;
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $x )
{
$flag = true;
}
else
{
if ( $flag )
$count += 1;
$flag = false;
}
}
if ( $flag )
$count += 1;
return $count ;
}
$a = array ( 8, 25, 10, 19, 19,
18, 20, 11, 18 );
$n = sizeof( $a );
$x = 13;
echo countSegments( $a , $n , $x );
?>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Approach : Sliding Window
Using sliding window technique. Maintain a window that expands from left to right as long as the elements in the window are
greater than X. Whenever we encounter an element that is not greater than X, we close the current window and move the left pointer
of the window to the next position. This way, we ensure that we count non-overlapping segments.
Steps:
- Initialize a variable count = 0 to keep track of the number of segments.
- Initialize two pointers, left=0 and right=0, both pointing to the start of the array.
- Iterate while right < N.
- If the element at right index > X, increment right.
- If the element at right index <= X, calculate the length of the current segment as length = right – left.
- If length is greater than 0, increment count.
- Move the left pointer to the next position (i.e., left = right + 1).
- Increment right.
- Return the count as the result.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
int countSegmentsGreaterThanX(std::vector< int >& arr, int X)
{
int count = 0;
int left = 0, right = 0;
int N = arr.size();
while (right < N) {
if (arr[right] > X) {
right++;
}
else {
int length = right - left;
if (length > 0) {
count++;
}
left = right + 1;
right++;
}
}
int length = right - left;
if (length > 0) {
count++;
}
return count;
}
int main()
{
vector< int > arr = { 3, 4, 5, 6, 7, 2, 10, 11 };
int X = 5;
int segments = countSegmentsGreaterThanX(arr, X);
cout << segments << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
static int countSegmentsGreaterThanX(List<Integer> arr, int X) {
int count = 0 ;
int left = 0 , right = 0 ;
int N = arr.size();
while (right < N) {
if (arr.get(right) > X) {
right++;
} else {
int length = right - left;
if (length > 0 ) {
count++;
}
left = right + 1 ;
right++;
}
}
int length = right - left;
if (length > 0 ) {
count++;
}
return count;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<Integer>();
arr.add( 3 );
arr.add( 4 );
arr.add( 5 );
arr.add( 6 );
arr.add( 7 );
arr.add( 2 );
arr.add( 10 );
arr.add( 11 );
int X = 5 ;
int segments = countSegmentsGreaterThanX(arr, X);
System.out.println(segments);
}
}
|
Python3
def countSegmentsGreaterThanX(arr, X):
count = 0
left = 0
right = 0
N = len (arr)
while right < N:
if arr[right] > X:
right + = 1
else :
length = right - left
if length > 0 :
count + = 1
left = right + 1
right + = 1
length = right - left
if length > 0 :
count + = 1
return count
arr = [ 3 , 4 , 5 , 6 , 7 , 2 , 10 , 11 ]
X = 5
segments = countSegmentsGreaterThanX(arr, X)
print (segments)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountSegmentsGreaterThanX(List< int > arr, int X)
{
int count = 0;
int left = 0, right = 0;
int N = arr.Count;
while (right < N)
{
if (arr[right] > X)
{
right++;
}
else
{
int length = right - left;
if (length > 0)
{
count++;
}
left = right + 1;
right++;
}
}
int finalLength = right - left;
if (finalLength > 0)
{
count++;
}
return count;
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 3, 4, 5, 6, 7, 2, 10, 11 };
int X = 5;
int segments = CountSegmentsGreaterThanX(arr, X);
Console.WriteLine(segments);
}
}
|
Javascript
function countSegmentsGreaterThanX(arr, X) {
let count = 0;
let left = 0;
let right = 0;
const N = arr.length;
while (right < N) {
if (arr[right] > X) {
right++;
} else {
const length = right - left;
if (length > 0) {
count++;
}
left = right + 1;
right++;
}
}
const length = right - left;
if (length > 0) {
count++;
}
return count;
}
const arr = [3, 4, 5, 6, 7, 2, 10, 11];
const X = 5;
const segments = countSegmentsGreaterThanX(arr, X);
console.log(segments);
|
Time Complexity: O(N), where N is the size of the input array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...