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

 Live Demo

#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

Updated on: 01-Dec-2020

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements