You are on page 1of 1

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

array elements
Difficulty Level : Easy
Last Updated : 16 Sep, 2020
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.

Recommended: Please try your approach on {IDE} first, before moving on to the
solution.
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.

You might also like