- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

- Related Articles
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Count pairs with average present in the same array in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum size of sub-array that satisfies the given condition in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Maximum size of sub-array that satisfies the given condition in C++ program
- Count of words that are present in all the given sentences in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Count elements present in first array but not in second in C++
- Count the number of pairs that have column sum greater than row sum in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++