📌  相关文章
📜  计算可以表示为至少两个连续数组元素之和的数组元素

📅  最后修改于: 2021-10-27 07:13:45             🧑  作者: Mango

给定一个由[1, N]范围内的N 个整数组成的数组A[] ,任务是计算可以表示为两个或多个连续数组元素之和的数组元素(非相异)的计数。

例子:

朴素方法:为每个元素遍历给定数组,找到所有可能子数组的总和并检查任何子数组的总和是否等于当前元素的总和。如果发现为真,则增加计数。最后,打印获得的计数。
时间复杂度: O(n 3 )
辅助空间: O(1)

高效方法:按照以下步骤优化上述方法:

  • 初始化一个数组cnt[]来存储每个数组元素出现的次数。
  • 迭代所有长度至少为 2 的子数组,保持当前子数组总和的总和。
  • 如果当前总和不超过N ,则将 cnt[sum] 添加到答案并设置 cnt[sum]=0 以防止多次计算相同的元素。
  • 最后,打印得到的总和。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find the number of
// array elements that can be
// represented as the sum of two
// or more consecutive array elements
int countElements(int a[], int n)
{
     
    // Stores the frequencies
    // of array elements
    int cnt[n + 1] = {0};
    memset(cnt, 0, sizeof(cnt));
     
    // Stores required count
    int ans = 0;
 
    // Update frequency of
    // each array element
    for(int i = 0; i < n; i++)
    {
        ++cnt[a[i]];
    }
     
    // Find sum of all subarrays
    for(int l = 0; l < n; ++l)
    {
        int sum = 0;
 
        for(int r = l; r < n; ++r)
        {
            sum += a[r];
 
            if (l == r)
                continue;
 
            if (sum <= n)
            {
                 
                // Increment ans by cnt[sum]
                ans += cnt[sum];
 
                // Reset cnt[sum] by 0
                cnt[sum] = 0;
            }
        }
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
     
    // Given array
    int a[] = { 1, 1, 1, 1, 1 };
    int N = sizeof(a) / sizeof(a[0]);
 
    // Function call
    cout << countElements(a, N);
}
 
// This code is contributed by Amit Katiyar


Java
// Java Program for above approach
 
import java.util.*;
class GFG {
 
    // Function to find the number of array
    // elements that can be represented as the sum
    // of two or more consecutive array elements
    static int countElements(int[] a, int n)
    {
        // Stores the frequencies
        // of array elements
        int[] cnt = new int[n + 1];
 
        // Stores required count
        int ans = 0;
 
        // Update frequency of
        // each array element
        for (int k : a) {
            ++cnt[k];
        }
 
        // Find sum of all subarrays
        for (int l = 0; l < n; ++l) {
 
            int sum = 0;
 
            for (int r = l; r < n; ++r) {
                sum += a[r];
 
                if (l == r)
                    continue;
 
                if (sum <= n) {
 
                    // Increment ans by cnt[sum]
                    ans += cnt[sum];
 
                    // Reset cnt[sum] by 0
                    cnt[sum] = 0;
                }
            }
        }
 
        // Return ans
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array
        int[] a = { 1, 1, 1, 1, 1 };
 
        // Function call
        System.out.println(
            countElements(a, a.length));
    }
}


Python3
# Python3 program for above approach
 
# Function to find the number of array
# elements that can be represented as the sum
# of two or more consecutive array elements
def countElements(a, n):
     
    # Stores the frequencies
    # of array elements
    cnt = [0] * (n + 1)
 
    # Stores required count
    ans = 0
 
    # Update frequency of
    # each array element
    for k in a:
        cnt[k] += 1
 
    # Find sum of all subarrays
    for l in range(n):
        sum = 0
 
        for r in range(l, n):
            sum += a[r]
 
            if (l == r):
                continue
            if (sum <= n):
 
                # Increment ans by cnt[sum]
                ans += cnt[sum]
 
                # Reset cnt[sum] by 0
                cnt[sum] = 0
 
    # Return ans
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Given array
    a = [ 1, 1, 1, 1, 1 ]
 
    # Function call
    print(countElements(a, len(a)))
 
# This code is contributed by mohit kumar 29


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to find the number of array
// elements that can be represented as the sum
// of two or more consecutive array elements
static int countElements(int[] a, int n)
{
     
    // Stores the frequencies
    // of array elements
    int[] cnt = new int[n + 1];
 
    // Stores required count
    int ans = 0;
 
    // Update frequency of
    // each array element
    foreach(int k in a)
    {
        ++cnt[k];
    }
 
    // Find sum of all subarrays
    for(int l = 0; l < n; ++l)
    {
        int sum = 0;
 
        for(int r = l; r < n; ++r)
        {
            sum += a[r];
            if (l == r)
                continue;
 
            if (sum <= n)
            {
                 
                // Increment ans by cnt[sum]
                ans += cnt[sum];
 
                // Reset cnt[sum] by 0
                cnt[sum] = 0;
            }
        }
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array
    int[] a = { 1, 1, 1, 1, 1 };
 
    // Function call
    Console.WriteLine(countElements(a, a.Length));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
0

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程