Count array elements that can be represented as sum of at least two consecutive array elements

Given an array A[] consisting of N integers from a range [1, N], the task is to calculate the count of array elements (non-distinct) that can be represented as the sum of two or more consecutive array elements.

Examples:

Input: a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5}
Output: 5
Explanation:
The array elements satisfying the condition are: 
4 = 3 + 1 
5 = 1 + 4 or 4 + 1 
9 = 3 + 1 + 4 + 1 
6 = 1 + 5 or 1 + 4 + 1 
5 = 1 + 4 or 4 + 1

Input: a[] = {1, 1, 1, 1, 1}
Output: 0
Explanation:
No such array element exists that can be represented as the sum of two or more consecutive elements.

Naive Approach: Traverse the given array for each element, find the sum of all possible subarrays and check if sum of any of the subarrays becomes equal to that of the current element. Increase count if found to be true. Finally, print the count obtained. 



Time Complexity: O(n3
Auxiliary Space: O(1)

Efficient Approach: Follow the steps below to optimize the above approach:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for above approach 
#include <bits/stdc++.h>
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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output: 
0

Time Complexity: O(N2)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :