Count sub-arrays which have elements less than or equal to X
Given an array of n elements and an integer X. Count the number of sub-arrays of this array which have all elements less than or equal to X.
Examples:
Input : arr[] = {1, 5, 7, 8, 2, 3, 9}
X = 6
Output : 6
Explanation : Sub-arrays are {1}, {5}, {2}, {3},
{1, 5}, {2, 3}
Input : arr[] = {1, 10, 12, 4, 5, 3, 2, 7}
X = 9
Output : 16
Naive Approach : A simple approach uses two nested loops for generating all sub-arrays of the given an array and a loop to check whether all elements of a sub-array is less than or equal to X or not.
Time Complexity: O(n*n*n)
Efficient Approach: An efficient approach is to observe that we just want the count of those sub-arrays which have all elements less than or equal to X. We can create a binary array of 0s and 1s corresponding to the original array. If an element in the original is less than or equal to X, then the corresponding element in the binary array will be 1 otherwise 0. Now, our problem reduces to count the number of sub-arrays in this binary array which has all 1s. We can also see that for an array which has all 1s all of its sub-arrays will have only 1s and the total number of sub-arrays will be len*(len+1)/2. For example, {1, 1, 1, 1} will have 10 sub-arrays.
Below is the complete algorithm to solve the above problem:
- Create a corresponding binary array of the original array as described above.
- Initialize a counter variable to 0 and start traversing the binary array keeping track of the lengths of sub-arrays which has all 1s
- We can easily calculate the number of sub-arrays of an array which has all 1s by using the formula n*(n+1)/2, where n is the length of the array with all 1s.
- Calculate the length of every sub-array which has all 1s and increment the count variable by length*(length+1)/2. We can do this in O(n) time complexity
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
int countSubArrays( int arr[], int n, int x)
{
int len = 0;
int count = 0;
int binaryArr[n];
for ( int i = 0; i < n; i++) {
if (arr[i] <= x)
binaryArr[i] = 1;
else
binaryArr[i] = 0;
}
for ( int i = 0; i < n; i++) {
if (binaryArr[i] == 1) {
int j;
for (j = i + 1; j < n; j++)
if (binaryArr[j] != 1)
break ;
len = j - i;
count += (len) * (len + 1) / 2;
i = j;
}
}
return count;
}
int main()
{
int arr[] = { 1, 5, 7, 8, 2, 3, 9 };
int x = 6;
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSubArrays(arr, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSubArrays( int arr[], int n, int x)
{
int len = 0 ;
int count = 0 ;
int binaryArr[] = new int [n];
for ( int i = 0 ; i < n; i++) {
if (arr[i] <= x)
binaryArr[i] = 1 ;
else
binaryArr[i] = 0 ;
}
for ( int i = 0 ; i < n; i++) {
if (binaryArr[i] == 1 ) {
int j;
for (j = i + 1 ; j < n; j++)
if (binaryArr[j] != 1 )
break ;
len = j - i;
count += (len) * (len + 1 ) / 2 ;
i = j;
}
}
return count;
}
public static void main(String args[])
{
int arr[] = { 1 , 5 , 7 , 8 , 2 , 3 , 9 };
int x = 6 ;
int n = arr.length;
System.out.println(countSubArrays(arr, n, x));
}
}
|
Python3
def countSubArrays(arr, n, x):
len = 0
count = 0
binaryArr = [ 0 for i in range (n)]
for i in range ( 0 , n, 1 ):
if (arr[i] < = x):
binaryArr[i] = 1
else :
binaryArr[i] = 0
for i in range ( 0 , n, 1 ):
if (binaryArr[i] = = 1 ):
for j in range (i + 1 , n, 1 ):
if (binaryArr[j] ! = 1 ):
break
len = j - i
count + = ( len ) * ( int )(( len + 1 ) / 2 )
i = j
return count
if __name__ = = '__main__' :
arr = [ 1 , 5 , 7 , 8 , 2 , 3 , 9 ]
x = 6
n = len (arr)
print ( int (countSubArrays(arr, n, x)))
|
C#
using System;
class GFG {
static int countSubArrays( int []arr,
int n, int x)
{
int len = 0;
int count = 0;
int []binaryArr = new int [n];
for ( int i = 0; i < n; i++) {
if (arr[i] <= x)
binaryArr[i] = 1;
else
binaryArr[i] = 0;
}
for ( int i = 0; i < n; i++) {
if (binaryArr[i] == 1) {
int j;
for (j = i + 1; j< n; j++)
if (binaryArr[j] != 1)
break ;
len = j - i;
count += (len) * (len + 1) / 2;
i = j;
}
}
return count;
}
public static void Main()
{
int []arr = { 1, 5, 7, 8, 2, 3, 9 };
int x = 6;
int n = arr.Length;
Console.WriteLine(
countSubArrays(arr, n, x));
}
}
|
PHP
<?php
function countSubArrays( $arr , $n , $x )
{
$len = 0;
$coun = 0;
$binaryArr = array ( $n );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] <= $x )
$binaryArr [ $i ] = 1;
else
$binaryArr [ $i ] = 0;
}
for ( $i = 0; $i < $n ; $i ++)
{
if ( $binaryArr [ $i ] == 1) {
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $binaryArr [ $j ] != 1)
break ;
$len = $j - $i ;
$coun += ( $len ) * ( $len + 1) / 2;
$i = $j ;
}
}
return $coun ;
}
$arr = array ( 1, 5, 7, 8, 2, 3, 9 );
$x = 6;
$n = count ( $arr );
echo countSubArrays( $arr , $n , $x );
?>
|
Javascript
<script>
function countSubArrays(arr , n , x) {
var len = 0;
var count = 0;
var binaryArr = Array(n).fill(0);
for (i = 0; i < n; i++) {
if (arr[i] <= x)
binaryArr[i] = 1;
else
binaryArr[i] = 0;
}
for (i = 0; i < n; i++) {
if (binaryArr[i] == 1) {
var j;
for (j = i + 1; j < n; j++)
if (binaryArr[j] != 1)
break ;
len = j - i;
count += (len) * (len + 1) / 2;
i = j;
}
}
return count;
}
var arr = [ 1, 5, 7, 8, 2, 3, 9 ];
var x = 6;
var n = arr.length;
document.write(countSubArrays(arr, n, x));
</script>
|
Output:
6
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(n).
Another Method: We can improve the above solution without using extra space keeping the time complexity O(n). Instead of marking elements as 0 and 1 we can keep track of start and end of each such region and update the count whenever the region ends.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countSubArrays( int arr[], int x, int n )
{
int count = 0;
int start = -1, end = -1;
for ( int i = 0; i < n; i++)
{
if (arr[i] < x)
{
if (start == -1)
{
start = i;
end = i;
}
else
{
end=i;
}
}
else
{
if (start != -1 && end != -1)
{
int length = end - start + 1;
count = count + ((length * (length + 1)) / 2);
}
start = -1;
end = -1;
}
}
if (start != -1 && end != -1)
{
int length = end - start + 1;
count = count + ((length * (length + 1)) / 2);
}
return count;
}
int main()
{
int arr[] = { 1, 5, 7, 8, 2, 3, 9 };
int x = 6;
int n = sizeof (arr) / sizeof (arr[0]);
cout<< countSubArrays(arr, x, n);
}
|
Java
public class GFG {
public static int countSubArrays( int arr[], int x)
{
int count = 0 ;
int start = - 1 , end = - 1 ;
for ( int i = 0 ; i < arr.length; i++)
{
if (arr[i] < x)
{
if (start == - 1 )
{
start = i;
end = i;
}
else
{
end=i;
}
}
else
{
if (start != - 1 && end != - 1 )
{
int length = end - start + 1 ;
count = count + ((length * (length + 1 )) / 2 );
}
start = - 1 ;
end = - 1 ;
}
}
if (start != - 1 && end != - 1 )
{
int length = end - start + 1 ;
count = count + ((length * (length + 1 )) / 2 );
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 7 , 8 , 2 , 3 , 9 };
int x = 6 ;
System.out.println(countSubArrays(arr, x));
}
}
|
Python3
def countSubArrays(arr, x, n ):
count = 0 ;
start = - 1 ; end = - 1 ;
for i in range (n):
if (arr[i] < x):
if (start = = - 1 ):
start = i;
end = i;
else :
end = i;
else :
if (start ! = - 1 and end ! = - 1 ):
length = end - start + 1 ;
count = count + ((length *
(length + 1 )) / 2 );
start = - 1 ;
end = - 1 ;
if (start ! = - 1 and end ! = - 1 ):
length = end - start + 1 ;
count = count + ((length *
(length + 1 )) / 2 );
return count;
arr = [ 1 , 5 , 7 , 8 , 2 , 3 , 9 ];
x = 6 ;
n = len (arr);
print (countSubArrays(arr, x, n));
|
C#
using System;
class GFG
{
public static int countSubArrays( int []arr, int x)
{
int count = 0;
int start = -1, end = -1;
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] < x)
{
if (start == -1)
{
start = i;
end = i;
}
else
{
end=i;
}
}
else
{
if (start != -1 && end != -1)
{
int length = end - start + 1;
count = count + ((length * (length + 1)) / 2);
}
start = -1;
end = -1;
}
}
if (start != -1 && end != -1)
{
int length = end - start + 1;
count = count + ((length * (length + 1)) / 2);
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 7, 8, 2, 3, 9 };
int x = 6;
Console.WriteLine(countSubArrays(arr, x));
}
}
|
Javascript
<script>
function countSubArrays(arr, x)
{
let count = 0;
let start = -1, end = -1;
for (let i = 0; i < arr.length; i++)
{
if (arr[i] < x)
{
if (start == -1)
{
start = i;
end = i;
}
else
{
end=i;
}
}
else
{
if (start != -1 && end != -1)
{
let length = end - start + 1;
count = count + parseInt((length * (length + 1)) / 2, 10);
}
start = -1;
end = -1;
}
}
if (start != -1 && end != -1)
{
let length = end - start + 1;
count = count + parseInt((length * (length + 1)) / 2, 10);
}
return count;
}
let arr = [ 1, 5, 7, 8, 2, 3, 9 ];
let x = 6;
document.write(countSubArrays(arr, x));
</script>
|
Output:
6
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1).
Last Updated :
25 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...