📌  相关文章
📜  计算将数组分为两半且总和相同的方式的数目

📅  最后修改于: 2021-05-04 17:45:56             🧑  作者: Mango

给定一个由N个元素组成的整数数组。任务是找到将数组拆分为两个非零长度的相等和子数组的方法。

例子:

简单的解决方案:一个简单的解决方案是生成所有可能的连续子数组对,然后在其中求和。如果它们的总和相同,我们将计数加一。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:这个想法是采用一个辅助数组,即aux []来计算数组的和,以便对于索引i aux [i]将存储从索引0到索引i的所有元素的总和。
通过这样做,我们可以在恒定时间内为数组的每个索引计算左和右。
因此,该想法是:

  1. 查找数组中所有数字的总和,并将其存储在变量S中。如果总和为奇数,则答案将为0。
  2. 遍历数组并继续计算元素之和。在第i步,我们将使用变量S来维护从索引0到i的所有元素的和。
    • 计算总和,直到第ith个索引。
    • 如果该总和等于S / 2,则将路数计数增加1。
  3. 从i = 0到i = N-2执行此操作。

下面是上述方法的实现:

C++
// C++ program to count the number of ways to
// divide an array into two halves
// with the same sum
 
#include 
using namespace std;
 
// Function to count the number of ways to
// divide an array into two halves
// with same sum
int cntWays(int arr[], int n)
{
    // if length of array is 1
    // answer will be 0 as we have
    // to split it into two
    // non-empty halves
    if (n == 1)
        return 0;
 
    // variables to store total sum,
    // current sum and count
    int tot_sum = 0, sum = 0, ans = 0;
 
    // finding total sum
    for (int i = 0; i < n; i++)
        tot_sum += arr[i];
 
    // checking if sum equals total_sum/2
    for (int i = 0; i < n - 1; i++) {
        sum += arr[i];
        if (sum == tot_sum / 2)
            ans++;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, -1, 1, -1, 1, -1 };
 
    int n = sizeof(arr) / sizeof(int);
 
    cout << cntWays(arr, n);
 
    return 0;
}


Java
// Java program to count the number of ways to
// divide an array into two halves
// with the same sum
class GFG
{
 
    // Function to count the number of ways to
    // divide an array into two halves
    // with same sum
    static int cntWays(int arr[], int n)
    {
        // if length of array is 1
        // answer will be 0 as we have
        // to split it into two
        // non-empty halves
        if (n == 1)
        {
            return 0;
        }
 
        // variables to store total sum,
        // current sum and count
        int tot_sum = 0, sum = 0, ans = 0;
 
        // finding total sum
        for (int i = 0; i < n; i++)
        {
            tot_sum += arr[i];
        }
 
        // checking if sum equals total_sum/2
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
            if (sum == tot_sum / 2)
            {
                ans++;
            }
        }
 
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = {1, -1, 1, -1, 1, -1};
 
        int n = arr.length;
 
        System.out.println(cntWays(arr, n));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python program to count the number of ways to
# divide an array into two halves
# with the same sum
 
# Function to count the number of ways to
# divide an array into two halves
# with same sum
def cntWays(arr, n):
    # if length of array is 1
    # answer will be 0 as we have
    # to split it into two
    # non-empty halves
    if (n == 1):
        return 0;
 
    # variables to store total sum,
    # current sum and count
    tot_sum = 0; sum = 0; ans = 0;
 
    # finding total sum
    for i in range(0,n):
        tot_sum += arr[i];
 
    # checking if sum equals total_sum/2
    for i in range(0,n-1):
        sum += arr[i];
        if (sum == tot_sum / 2):
            ans+=1;
    return ans;
 
# Driver Code
arr = [1, -1, 1, -1, 1, -1 ];
 
n = len(arr);
 
print(cntWays(arr, n));
 
# This code contributed by PrinciRaj1992


C#
// C# program to count the number of ways to
// divide an array into two halves with
// the same sum
using System;
 
class GFG
{
 
    // Function to count the number of ways to
    // divide an array into two halves
    // with same sum
    static int cntWays(int []arr, int n)
    {
        // if length of array is 1
        // answer will be 0 as we have
        // to split it into two
        // non-empty halves
        if (n == 1)
        {
            return 0;
        }
 
        // variables to store total sum,
        // current sum and count
        int tot_sum = 0, sum = 0, ans = 0;
 
        // finding total sum
        for (int i = 0; i < n; i++)
        {
            tot_sum += arr[i];
        }
 
        // checking if sum equals total_sum/2
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
            if (sum == tot_sum / 2)
            {
                ans++;
            }
        }
 
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        int []arr = {1, -1, 1, -1, 1, -1};
 
        int n = arr.Length;
 
        Console.WriteLine(cntWays(arr, n));
    }
}
 
// This code contributed by anuj_67..


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)