- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++

Given an array arr[ ] of positive integers. The goal is to find the count of subarrays of arr[ ] that have an average of its elements greater than the average of the rest of the elements of arr[ ] that are not present in it.

**For Example**

## Input

arr[ ] = { 3, 2, 4 }

## Output

Count of number of sub-arrays such that the average of elements present in the sub−array is greater than that do not present in the sub−array are: 2

## Explanation

The subarrays are − [ 3 ], [ 2 ], [ 4 ], [ 3,2 ], [ 2,4 ], [ 3,2,4 ]. Average of [ 4 ] is 4 which is more than the average of [ 2,3 ]. Average of [ 3,2,4 ] is 3 which is more than the average of [ ]

## Input

arr[ ] = { 3, 3, 3 }

## Output

Count of number of sub−arrays such that the average of elements present in the sub−array is greater than that do not present in the sub−array are: 1

## Explanation

The subarrays are − [ 3 ], [ 3 ], [ 3 ], [ 3,3 ], [ 3,3 ], [ 3,3,3 ]. Average of [ 3,3,3 ] is 3 which is more than the average of [ ]

**Approach used in the below program is as follows** −

In this approach create a prefix sum array which will store the sum of elements upto index i in new_arr[i] . Now we have sums upto previous element then calculate sum upto arr[i] and count of elements as j−i+1 and calculate averages.

Take an array arr[] as input.

Function count(int arr[], int size) takes arr[ ] and returns the count of sub−arrays such that the average of elements present in the sub−array is greater than that not present in the sub−array.

Take an array new_arr[size] to store sum upto previous index elements.

Traverse from i=0 to i<size and set new_arr[i] with new_arr[i−1]+arr[i−1].

Now traverse new_arr[ ] using two for loops.

Now calculate total_1 as the sum of previous subarrays. And elements in it as count_1.

Calculate total_2 as sum of next subarrays and elements in it as count_2.

Calculate averages as check_1 = total_1 / count_1; and check_2 = total_2 / count_2;

If average check_1 > check_2 then increment count.

At the end of for loops return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int count(int arr[], int size){ int count = 0; int new_size = size + 1; int new_arr[new_size] = { 0 }; for (int i = 1; i < new_size; i++){ new_arr[i] = new_arr[i − 1] + arr[i − 1]; } for (int i = 1; i < new_size; i++){ for (int j = i; j < new_size; j++){ int total_1 = new_arr[j] − new_arr[i − 1]; int count_1 = j − i + 1; int total_2 = new_arr[size] − total_1; int count_2 = 0; if((size − count_1) == 0){ count_2 = 1; } else { count_2 = size − count_1; } int check_1 = total_1 / count_1; int check_2 = total_2 / count_2; if (check_1 > check_2){ count++; } } } return count; } int main(){ int arr[] = { 2, 6, 2, 4 }; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of 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 are: "<<count(arr, size); return 0; }

## Output

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

Count the number of sub−arrays such that the average of elements present in the subarrayis greater than that not present in the sub-array are: 6