给定N个正整数的数组arr [] ,任务是计算所有子数组,其中子数组元素的总和严格大于其余元素的总和。
例子:
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} = 6
Input: 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
天真的方法:
天真的方法是使用三个嵌套循环生成每个子数组的总和,并使用剩余数组元素的总和检查计算出的子数组总和。
- 第一个循环指示子数组的开始。
- 第二个循环指示子数组的结尾。
- 在第二个循环中,我们有for循环来计算subarray_sum和剩余的数组元素总和。
- 当subarray_sum严格大于remaining_sum时,增加计数器。
下面是上述方法的实现:
CPP
// C++ implementation of the above approach
#include
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
// 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
Python3
# 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#
// 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 begining 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
C++
// C++ implementation of the above approach
#include
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 begining 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
// 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 begining 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
# 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 begining 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 begining 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
5
时间复杂度: O(N 3 )
高效方法:
一种有效的解决方案是使用给定数组arr []的total_sum,这有助于计算subarray_sum和剩余的sum。
- 计算给定数组的总和。
- 运行一个for循环,其中循环变量i指示子数组的起始索引。
- 另一个循环,其中每个j表示子数组的结束索引,并为每个第j个索引计算subarray_sum。
- subarray_sum = arr [i] + arr [i + 1] + ….. + arr [j]
剩余的总和= total_sum – subarray_sum - 然后,当子数组总和严格大于数组元素的剩余总和时,检查条件和增量计数器。
下面是上述方法的实现。
C++
// C++ implementation of the above approach
#include
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 begining 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
// 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 begining 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
# 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 begining 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 begining 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
5
时间复杂度: O(N 2 )