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++ 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 array 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 code int main()
{ int arr[] = { 10, 9, 12, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << Count_subarray(arr, n);
return 0;
} |
// Java implementation of the above approach import java.util.*;
class GFG
{ // Function to count the number of // sub-arrays with sum strictly greater // than the remaining elements of array static 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 code public 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 |
# Python implementation of the above approach # Function to count the number of # sub-arrays with sum strictly greater # than the remaining elements of array def 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 code if __name__ = = '__main__' :
arr = [ 10 , 9 , 12 , 6 ];
n = len (arr);
print (Count_subarray(arr, n));
# This code is contributed by 29AjayKumar |
// C# implementation of the above approach using System;
class GFG
{ // Function to count the number of // sub-arrays with sum strictly greater // than the remaining elements of array static 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 code public 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 |
<script> // Javascript implementation of // the above approach // Function to count the number of // sub-arrays with sum strictly greater // than the remaining elements of array function 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++ 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 code int main()
{ int arr[] = { 10, 9, 12, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << Count_subarray(arr, n);
return 0;
} |
// Java implementation of the above approach 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(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 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# 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 |
<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)