Count the subarray with sum strictly greater than the sum of remaining elements

Given an array arr[] of N positive integers, the task is to count all the subarrays where the sum of subarray elements is strictly greater than the sum of remaining elements.
Examples:Â
Input: arr[] = {1, 2, 3, 4, 5}Â
Output: 6Â
Explanation:Â
Subarrays are:Â
{1, 2, 3, 4} – sum of subarray = 10, sum of remaining elements {5} = 5Â
{1, 2, 3, 4, 5} – sum of subarray =15, sum of remaining element = 0Â
{2, 3, 4} – sum of subarray = 9, sum of remaining elements {1, 5} = 6Â
{2, 3, 4, 5} – sum of subarray = 14, sum of remaining elements {1} = 1Â
{3, 4, 5} – sum of subarray = 12, sum of remaining elements {1, 2} = 3Â
{4, 5} – sum of subarray = 9, sum of remaining elements {1, 2, 3} = 6Input: arr[] = {10, 9, 12, 6}Â
Output: 5Â
Explanation:Â
Sub arrays are :Â
{10, 9} – sum of subarray = 19, sum of remaining elements {12, 6} = 18Â
{10, 9, 12} – sum of subarray = 31, sum of remaining elements {6} = 6Â
{10, 9, 12, 6} – sum of subarray = 37, sum of remaining elements = 0Â
{9, 12} – sum of subarray = 21, sum of remaining elements {10, 6} = 16Â
{9, 12, 6} – sum of subarray =27, sum of remaining element {10} = 10Â
Â
Naive Approach:Â
A naive approach is to generate the sum of every subarray using three nested loops and check the calculated subarray sum with the sum of the remaining array elements.
- The first loop indicates the beginning of the subarray.
- The second loop indicates the ending of the subarray.
- Inside the second loop, we have for loops to calculate the subarray_sum and remaining array element sum.
- Increment the counter, when subarray_sum is strictly greater than remaining_sum.
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to count the number of// sub-arrays with sum strictly greater// than the remaining elements of arrayint Count_subarray(int arr[], int n){Â Â Â Â int subarray_sum, remaining_sum, count = 0;Â
    // For loop for beginning point of a subarray    for (int i = 0; i < n; i++) {Â
        // For loop for ending point of the subarray        for (int j = i; j < n; j++) {Â
            // Initialise subarray_sum and            // remaining_sum to 0            subarray_sum = 0;            remaining_sum = 0;Â
            // For loop to calculate            // the sum of generated subarray            for (int k = i; k <= j; k++) {                subarray_sum += arr[k];            }            // For loop to calculate the            // sum remaining array element            for (int l = 0; l < i; l++) {                remaining_sum += arr[l];            }            for (int l = j + 1; l < n; l++) {                remaining_sum += arr[l];            }            // Checking for condition when            // subarray sum is strictly greater than            // remaining sum of array element            if (subarray_sum > remaining_sum) {                count += 1;            }        }    }    return count;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 10, 9, 12, 6 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << Count_subarray(arr, n);Â Â Â Â return 0;} |
Java
// Java implementation of the above approachimport java.util.*;Â
class GFG{Â
// Function to count the number of// sub-arrays with sum strictly greater// than the remaining elements of arraystatic int Count_subarray(int arr[], int n){Â Â Â Â int subarray_sum, remaining_sum, count = 0;Â
    // For loop for beginning point of a subarray    for (int i = 0; i < n; i++)     {Â
        // For loop for ending point of the subarray        for (int j = i; j < n; j++)        {Â
            // Initialise subarray_sum and            // remaining_sum to 0            subarray_sum = 0;            remaining_sum = 0;Â
            // For loop to calculate            // the sum of generated subarray            for (int k = i; k <= j; k++)            {                subarray_sum += arr[k];            }                         // For loop to calculate the            // sum remaining array element            for (int l = 0; l < i; l++)             {                remaining_sum += arr[l];            }            for (int l = j + 1; l < n; l++)            {                remaining_sum += arr[l];            }                         // Checking for condition when            // subarray sum is strictly greater than            // remaining sum of array element            if (subarray_sum > remaining_sum)            {                count += 1;            }        }    }    return count;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int arr[] = { 10, 9, 12, 6 };Â Â Â Â int n = arr.length;Â Â Â Â System.out.print(Count_subarray(arr, n));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python implementation of the above approachÂ
# Function to count the number of# sub-arrays with sum strictly greater# than the remaining elements of arraydef Count_subarray(arr, n):Â Â Â Â subarray_sum, remaining_sum, count = 0, 0, 0;Â
    # For loop for beginning point of a subarray    for i in range(n):Â
        # For loop for ending point of the subarray        for j in range(i, n):Â
            # Initialise subarray_sum and            # remaining_sum to 0            subarray_sum = 0;            remaining_sum = 0;Â
            # For loop to calculate            # the sum of generated subarray            for k in range(i, j + 1):                subarray_sum += arr[k];             Â
            # For loop to calculate the            # sum remaining array element            for l in range(i):                remaining_sum += arr[l];            for l in range(j + 1, n):                remaining_sum += arr[l];                         # Checking for condition when            # subarray sum is strictly greater than            # remaining sum of array element            if (subarray_sum > remaining_sum):                count += 1;                 return count;Â
# Driver codeif __name__ == '__main__':Â Â Â Â arr = [ 10, 9, 12, 6];Â Â Â Â n = len(arr);Â Â Â Â print(Count_subarray(arr, n));Â Â Â Â Â # This code is contributed by 29AjayKumar |
C#
// C# implementation of the above approachusing System;Â
class GFG{Â
// Function to count the number of// sub-arrays with sum strictly greater// than the remaining elements of arraystatic int Count_subarray(int []arr, int n){Â Â Â Â int subarray_sum, remaining_sum, count = 0;Â
    // For loop for beginning point of a subarray    for (int i = 0; i < n; i++)     {Â
        // For loop for ending point of the subarray        for (int j = i; j < n; j++)        {Â
            // Initialise subarray_sum and            // remaining_sum to 0            subarray_sum = 0;            remaining_sum = 0;Â
            // For loop to calculate            // the sum of generated subarray            for (int k = i; k <= j; k++)            {                subarray_sum += arr[k];            }                         // For loop to calculate the            // sum remaining array element            for (int l = 0; l < i; l++)             {                remaining_sum += arr[l];            }            for (int l = j + 1; l < n; l++)            {                remaining_sum += arr[l];            }                         // Checking for condition when            // subarray sum is strictly greater than            // remaining sum of array element            if (subarray_sum > remaining_sum)            {                count += 1;            }        }    }    return count;}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int []arr = { 10, 9, 12, 6 };Â Â Â Â int n = arr.Length;Â Â Â Â Console.Write(Count_subarray(arr, n));}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>// Javascript implementation of// the above approachÂ
// Function to count the number of// sub-arrays with sum strictly greater// than the remaining elements of arrayfunction Count_subarray(arr, n){   var subarray_sum, remaining_sum,        count = 0;          var i,j,k,l;    // For loop for beginning     // point of a subarray    for(i = 0; i < n; i++) {Â
        // For loop for ending point         // of the subarray        for (j = i; j < n; j++) {Â
            // Initialise subarray_sum and            // remaining_sum to 0            subarray_sum = 0;            remaining_sum = 0;Â
            // For loop to calculate            // the sum of generated subarray            for(k = i; k <= j; k++) {                subarray_sum += arr[k];            }            // For loop to calculate the            // sum remaining array element            for (l = 0; l < i; l++) {                remaining_sum += arr[l];            }            for (l = j + 1; l < n; l++) {                remaining_sum += arr[l];            }            // Checking for condition when            // subarray sum is strictly             // greater than            // remaining sum of array element            if (subarray_sum > remaining_sum)             {                count += 1;            }        }    }    return count;}Â
// Driver code    var arr = [10, 9, 12, 6];    var n = arr.length;    document.write(Count_subarray(arr, n));Â
</script> |
5
Â
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:
An efficient solution is to use the total sum of given array arr[] that helps in calculating subarray_sum and remaining_sum.Â
- The total sum of the given array is calculated.
- Run a for loop where the loop variable i indicate the beginning index of subarray.
- Another loop, where every j indicate the ending index of the subarray and calculate subarray_sum for every j th index.
- subarray_sum=arr[i]+arr[i+1]+…..+arr[j]Â
remaining_sum=total_sum – subarray_sum - Then, check for condition and increment counter when the subarray sum is strictly greater than the remaining sum of array elements.
Below is the implementation of the above approach.
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
int Count_subarray(int arr[], int n){    int total_sum = 0, subarray_sum,        remaining_sum, count = 0;Â
    // Calculating total sum of given array    for (int i = 0; i < n; i++) {        total_sum += arr[i];    }Â
    // For loop for beginning point of a subarray    for (int i = 0; i < n; i++) {        // initialise subarray_sum to 0        subarray_sum = 0;Â
        // For loop for calculating        // subarray_sum and remaining_sum        for (int j = i; j < n; j++) {Â
            // Calculating subarray_sum            // and corresponding remaining_sum            subarray_sum += arr[j];            remaining_sum = total_sum - subarray_sum;Â
            // Checking for the condition when            // subarray sum is strictly greater than            // the remaining sum of the array element            if (subarray_sum > remaining_sum) {                count += 1;            }        }    }    return count;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 10, 9, 12, 6 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << Count_subarray(arr, n);Â Â Â Â return 0;} |
Java
// Java implementation of the above approachclass GFG{Â
static int Count_subarray(int arr[], int n){    int total_sum = 0, subarray_sum,        remaining_sum, count = 0;Â
    // Calculating total sum of given array    for (int i = 0; i < n; i++)    {        total_sum += arr[i];    }Â
    // For loop for beginning point of a subarray    for (int i = 0; i < n; i++)     {        // initialise subarray_sum to 0        subarray_sum = 0;Â
        // For loop for calculating        // subarray_sum and remaining_sum        for (int j = i; j < n; j++)        {Â
            // Calculating subarray_sum            // and corresponding remaining_sum            subarray_sum += arr[j];            remaining_sum = total_sum - subarray_sum;Â
            // Checking for the condition when            // subarray sum is strictly greater than            // the remaining sum of the array element            if (subarray_sum > remaining_sum)             {                count += 1;            }        }    }    return count;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int arr[] = { 10, 9, 12, 6 };Â Â Â Â int n = arr.length;Â Â Â Â System.out.print(Count_subarray(arr, n));}}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the above approach Â
def Count_subarray(arr, n) : Â
    total_sum = 0;     count = 0; Â
    # Calculating total sum of given array     for i in range(n) :        total_sum += arr[i];          # For loop for beginning point of a subarray     for i in range(n) : Â
        # initialise subarray_sum to 0         subarray_sum = 0; Â
        # For loop for calculating         # subarray_sum and remaining_sum         for j in range(i, n) : Â
            # Calculating subarray_sum             # and corresponding remaining_sum             subarray_sum += arr[j];             remaining_sum = total_sum - subarray_sum; Â
            # Checking for the condition when             # subarray sum is strictly greater than             # the remaining sum of the array element             if (subarray_sum > remaining_sum) :                count += 1;              return count; Â
# Driver code if __name__ == "__main__" : Â
    arr = [ 10, 9, 12, 6 ];     n = len(arr);     print(Count_subarray(arr, n)); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approach using System;Â
class GFG {          static int Count_subarray(int []arr, int n)     {         int total_sum = 0, subarray_sum,             remaining_sum, count = 0;              // Calculating total sum of given array         for (int i = 0; i < n; i++)         {             total_sum += arr[i];         }              // For loop for beginning point of a subarray         for (int i = 0; i < n; i++)         {             // initialise subarray_sum to 0             subarray_sum = 0;                  // For loop for calculating             // subarray_sum and remaining_sum             for (int j = i; j < n; j++)             {                      // Calculating subarray_sum                 // and corresponding remaining_sum                 subarray_sum += arr[j];                 remaining_sum = total_sum - subarray_sum;                      // Checking for the condition when                 // subarray sum is strictly greater than                 // the remaining sum of the array element                 if (subarray_sum > remaining_sum)                 {                     count += 1;                 }             }         }         return count;     }          // Driver code     public static void Main()     {         int []arr = { 10, 9, 12, 6 };         int n = arr.Length;         Console.WriteLine(Count_subarray(arr, n));     } } Â
// This code is contributed by AnkitRai01 |
Javascript
<script>// javascript implementation of the above approach   function Count_subarray(arr, n) {        var total_sum = 0, subarray_sum, remaining_sum, count = 0;Â
        // Calculating total sum of given array        for (i = 0; i < n; i++)        {            total_sum += arr[i];        }Â
        // For loop for beginning point of a subarray        for (i = 0; i < n; i++)        {                     // initialise subarray_sum to 0            subarray_sum = 0;Â
            // For loop for calculating            // subarray_sum and remaining_sum            for (j = i; j < n; j++)            {Â
                // Calculating subarray_sum                // and corresponding remaining_sum                subarray_sum += arr[j];                remaining_sum = total_sum - subarray_sum;Â
                // Checking for the condition when                // subarray sum is strictly greater than                // the remaining sum of the array element                if (subarray_sum > remaining_sum)                {                    count += 1;                }            }        }        return count;    }Â
    // Driver code           var arr = [ 10, 9, 12, 6 ];        var n = arr.length;        document.write(Count_subarray(arr, n));Â
// This code is contributed by todaysgaurav </script> |
5
Â
Time Complexity: O(N2)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


