- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Count sub-arrays which have elements less than or equal to X in C++

We are given an array arr[] containing integers and a variable X. The goal is to count all subarrays of arr[] such that each subarray contains only elements that are less than or equal to X. For example if array is [1,2,3] and X=2 then subarrays will be [1], [2] and [1,2]

Let us understand with examples.

**Input** − arr[] = { 4,3,2,1,6 }; X=3

**Output** − Count of sub-arrays which have elements less than or equal to X is − 6

**Explanation** − Subaarays will be −

[3], [2], [1], [3,2], [2,1], [3,2,1]

**Input** − arr[] = { 3,6,2,7,1,8,5 }; X=5

**Output** − Count of sub-arrays which have elements less than or equal to X is − 4

**Explanation** − Subaarays will be −

[3], [2], [1], [5]

## The approach used in the below program is as follows

We are creating a binary array temp_arr[] of the same size as the original array arr[]. This binary array will have 1 if corresponding arr[i] is less or equal to X, else 0. Now traverse temp_arr[] and check for continuous 1’s ( elements less than X in arr[] ). Store the length of each such subarray in temp. For an array of length temp. Total subarrays would be temp*(temp+1)/2. Add this to the total count and continue till the end of temp_arr[].

Take the array arr[] and variable X.

Function sub_X(int arr[], int size, int x) takes the array and x and returns a count of subarrays with only elements that are less than or equal to x.

Take the temporary variable temp and the final total of such subarrays as count.

Take a binary array temp_arr[] of length same as arr[].

We will traverse the array arr[] using for loop from i=0 to i<size.

For each element arr[i]<=x, set temp_arr[i]=1 else 0.

Traverse temp_arr[] using for loop.

If any element temp_arr[i] == 1. Then traverse using a sub loop from the current index i till temp_arr[temp_2] ( temp_2=i+1; temp_2<size ) is 1. If 0 then break the sub loop.

The count of subarray with all 1’s will be temp= temp_2-i.

This subarray has all 1’s which means all elements in arr[i] are <= x. Total subarrays will be temp_3= temp*(temp+1)/2.

At the end of both traversals, the count will have a total number of counts of all subarrays within arr that have numbers less than or equal to x.

## Example

#include <iostream> using namespace std; int sub_X(int arr[], int size, int x){ int count = 0, temp = 0; int temp_arr[size]; for (int i = 0; i < size; i++){ if (arr[i] <= x){ temp_arr[i] = 1; } else{ temp_arr[i] = 0; } } for (int i = 0; i < size; i++){ if (temp_arr[i] == 1){ int temp_2; for(temp_2 = i + 1; temp_2 < size; temp_2++){ if(temp_arr[temp_2] != 1){ break; } } temp = temp_2 - i; int temp_3 = (temp) * (temp + 1)/2; count = count + temp_3; i = temp_2; } } return count; } int main(){ int arr[] = { 2, 6, 1, 10, 5, 3 }; int x = 4; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of sub-arrays which have elements less than or equal to X are: "<<sub_X(arr, size, x); return 0; }

## Output

If we run the above code it will generate the following output −

Count of sub-arrays which have elements less than or equal to X are: 3

- Related Questions & Answers
- Count elements smaller than or equal to x in a sorted matrix in C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- 8085 program to count number of elements which are less than 0A
- Query results that have less than X characters in MySQL?
- Program to find X for special array with X elements greater than or equal X in Python
- Find Largest Special Prime which is less than or equal to a given number in C++
- Minimum swaps required to bring all elements less than or equal to k together in C++
- How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++
- Find all factorial numbers less than or equal to n in C++
- Print all prime numbers less than or equal to N in C++
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Python – Elements with factors count less than K
- Check which element in a masked array is less than or equal to a given value in Numpy