Count the 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

Given an array of integers arr[], the task is to count the number of sub-arrays such that the average of elements present in the sub-array is greater than the average of elements that are not present in the sub-array.
Examples:Â
Â
Input: arr[] = {6, 3, 5}Â
Output: 3Â
The sub-arrays are {6}, {5} and {6, 3, 5} because their averagesÂ
are greater than {3, 5}, {6, 3} and {} respectively.Â
Input: arr[] = {2, 1, 4, 1}Â
Output: 5Â
Â
Â
Approach: The problem can be solved easily by calculating the prefix sum array of the given array. The ith element of the prefix sum array will contain sum of elements up to i. So, the sum of elements between any two indexes i and j can be found using the prefix sum array. Using a nested loop, find all the possible sub-arrays such that its average sum is greater than average of elements not present in the array.Â
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count of sub-arrays// such that the average of elements present// in the sub-array is greater than the// average of the elements not present// in the sub-arrayint countSubarrays(int a[], int n){    // Initialize the count variable    int count = 0;Â
    // Initialize prefix sum array    int pre[n + 1] = { 0 };Â
    // Preprocessing prefix sum    for (int i = 1; i < n + 1; i++) {        pre[i] = pre[i - 1] + a[i - 1];    }Â
    for (int i = 1; i < n + 1; i++) {        for (int j = i; j < n + 1; j++) {Â
            // Calculating sum and count            // to calculate averages            int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;            int sum2 = pre[n] - sum1, count2 = ((n - count1) == 0) ? 1 : (n - count1);Â
            // Calculating averages            int includ = sum1 / count1;            int exclud = sum2 / count2;Â
            // Increment count if including avg            // is greater than excluding avg            if (includ > exclud)                count++;        }    }Â
    return count;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 6, 3, 5 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << countSubarrays(arr, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â
// Function to return the count of sub-arrays// such that the average of elements present// in the sub-array is greater than the// average of the elements not present// in the sub-arraystatic int countSubarrays(int a[], int n){    // Initialize the count variable    int count = 0;Â
    // Initialize prefix sum array    int []pre = new int[n + 1];    Arrays.fill(pre, 0);Â
    // Preprocessing prefix sum    for (int i = 1; i < n + 1; i++)    {        pre[i] = pre[i - 1] + a[i - 1];    }Â
    for (int i = 1; i < n + 1; i++)    {        for (int j = i; j < n + 1; j++)         {Â
            // Calculating sum and count            // to calculate averages            int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;            int sum2 = pre[n] - sum1, count2 =                 ((n - count1) == 0) ? 1 : (n - count1);Â
            // Calculating averages            int includ = sum1 / count1;            int exclud = sum2 / count2;Â
            // Increment count if including avg            // is greater than excluding avg            if (includ > exclud)                count++;        }    }    return count;}Â
// Driver codepublic static void main(String args[]){Â Â Â Â int arr[] = { 6, 3, 5 };Â Â Â Â int n = arr.length;Â Â Â Â System.out.println(countSubarrays(arr, n));}}Â
// This code is contributed by SURENDRA_GANGWAR |
Python3
# Python3 implementation of the approachÂ
# Function to return the count of sub-arrays# such that the average of elements present# in the sub-array is greater than the# average of the elements not present# in the sub-arraydef countSubarrays(a, n):         # Initialize the count variable    count = 0Â
    # Initialize prefix sum array    pre = [0 for i in range(n + 1)]Â
    # Preprocessing prefix sum    for i in range(1, n + 1):        pre[i] = pre[i - 1] + a[i - 1]Â
    for i in range(1, n + 1):        for j in range(i, n + 1):Â
            # Calculating sum and count            # to calculate averages            sum1 = pre[j] - pre[i - 1]            count1 = j - i + 1            sum2 = pre[n] - sum1Â
            if n-count1 == 0:                count2 = 1            else:                count2 = n - count1Â
            # Calculating averages            includ = sum1 // count1            exclud = sum2 // count2Â
            # Increment count if including avg            # is greater than excluding avg            if (includ > exclud):                count += 1             return countÂ
# Driver codearr = [6, 3, 5 ]n = len(arr)print(countSubarrays(arr, n))Â
# This code is contributed by mohit kumar |
C#
// C# implementation of the approachusing System;Â
class GFG{Â
// Function to return the count of sub-arrays// such that the average of elements present// in the sub-array is greater than the// average of the elements not present// in the sub-arraystatic int countSubarrays(int []a, int n){    // Initialize the count variable    int count = 0;Â
    // Initialize prefix sum array    int []pre = new int[n + 1];    Array.Fill(pre, 0);Â
    // Preprocessing prefix sum    for (int i = 1; i < n + 1; i++)    {        pre[i] = pre[i - 1] + a[i - 1];    }Â
    for (int i = 1; i < n + 1; i++)    {        for (int j = i; j < n + 1; j++)         {Â
            // Calculating sum and count            // to calculate averages            int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;            int sum2 = pre[n] - sum1, count2 =                 ((n - count1) == 0) ? 1 : (n - count1);Â
            // Calculating averages            int includ = sum1 / count1;            int exclud = sum2 / count2;Â
            // Increment count if including avg            // is greater than excluding avg            if (includ > exclud)                count++;        }    }    return count;}Â
// Driver codepublic static void Main(){Â Â Â Â int []arr = { 6, 3, 5 };Â Â Â Â int n = arr.Length;Â Â Â Â Console.WriteLine(countSubarrays(arr, n));}}Â
// This code is contributed by Akanksha Rai |
PHP
<?php// PHP implementation of the approach Â
// Function to return the count of sub-arrays // such that the average of elements present // in the sub-array is greater than the // average of the elements not present // in the sub-array function countSubarrays($a, $n) {     // Initialize the count variable     $count = 0; Â
    // Initialize prefix sum array     $pre = array_fill(0, $n + 1, 0); Â
    // Preprocessing prefix sum     for ($i = 1; $i < $n + 1; $i++)     {         $pre[$i] = $pre[$i - 1] + $a[$i - 1];     } Â
    for ($i = 1; $i < $n + 1; $i++)     {         for ($j = $i; $j < $n + 1; $j++)         { Â
            // Calculating sum and count             // to calculate averages             $sum1 = $pre[$j] - $pre[$i - 1] ;            $count1 = $j - $i + 1;             $sum2 = $pre[$n] - $sum1;             $count2 = (($n - $count1) == 0) ?                    1 : ($n - $count1); Â
            // Calculating averages             $includ = floor($sum1 / $count1);             $exclud = floor($sum2 / $count2); Â
            // Increment count if including avg             // is greater than excluding avg             if ($includ > $exclud)                 $count++;         }     } Â
    return $count; } Â
// Driver code $arr = array( 6, 3, 5 ); Â
$n = count($arr) ;Â
echo countSubarrays($arr, $n); Â
// This code is contributed by Ryuga?> |
Javascript
<script>Â
// JavaScript implementation of the approach Â
// Function to return the count of sub-arrays // such that the average of elements present // in the sub-array is greater than the // average of the elements not present // in the sub-array function countSubarrays(a, n) {     // Initialize the count variable     let count = 0; Â
    // Initialize prefix sum array     let pre = new Uint8Array(n + 1); Â
    // Preprocessing prefix sum     for (let i = 1; i < n + 1; i++) {         pre[i] = pre[i - 1] + a[i - 1];     } Â
    for (let i = 1; i < n + 1; i++) {         for (let j = i; j < n + 1; j++) { Â
            // Calculating sum and count             // to calculate averages             let sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;             let sum2 = pre[n] - sum1, count2 = ((n - count1) == 0) ? 1 : (n - count1); Â
            // Calculating averages             let includ = Math.floor(sum1 / count1);             let exclud = Math.floor(sum2 / count2); Â
            // Increment count if including avg             // is greater than excluding avg             if (includ > exclud)                 count++;         }     } Â
    return count; } Â
// Driver code     let arr = [ 6, 3, 5 ];     let n = arr.length;     document.write(countSubarrays(arr, n)); Â
Â
Â
// This code is contributed by Surbhi Tyagi.Â
</script> |
3
Â
Time Complexity: O(N^2) where N is the length of the array, as are using nested loops to traverse N*N times.
Auxiliary Space: O(N), as we are using pre array of size N while is extra space.
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



