- 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

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**

arr[ ] = { 3, 2, 4 }

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

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 [ ]

arr[ ] = { 3, 3, 3 }

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

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.

#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; }

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 Questions & Answers
- Count of sub-strings that do not contain all the in C++
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++
- Count pairs with average present in the same array in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Count of words that are present in all the given sentences in C++
- Check whether the Average Character of the String is present or not in Python
- Maximum size of sub-array that satisfies the given condition in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Count the number of pairs that have column sum greater than row sum in C++
- Maximum size of sub-array that satisfies the given condition in C++ program
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++
- Finding the sub array that has maximum sum JavaScript
- Convert the array such that the GCD of the array becomes 1 in C++

Advertisements