Count array elements that can be represented as sum of at least two consecutive array elements
Last Updated :
23 Jan, 2023
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.
C++
#include <bits/stdc++.h>
using namespace std;
int countElements( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int sum = arr[i];
bool flag = false ;
for ( int j = 0; j < i; j++) {
int reqSum = 0;
for ( int k = j; k < i; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
for ( int j = i + 1; j < n && flag == false ; j++) {
int reqSum = 0;
for ( int k = j; k < n; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
}
return count;
}
int main()
{
int a[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
int N = sizeof (a) / sizeof (a[0]);
cout << countElements(a, N);
}
|
Java
import java.io.*;
class GFG
{
public static int countElements( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int sum = arr[i];
boolean flag = false ;
for ( int j = 0 ; j < i; j++) {
int reqSum = 0 ;
for ( int k = j; k < i; k++) {
reqSum += arr[k];
if ((k - j + 1 ) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
for ( int j = i + 1 ; j < n && flag == false ;
j++) {
int reqSum = 0 ;
for ( int k = j; k < n; k++) {
reqSum += arr[k];
if ((k - j + 1 ) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
}
return count;
}
public static void main(String[] args)
{
int a[] = { 3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 };
int N = a.length;
System.out.println(countElements(a, N));
}
}
|
Python3
def countElements(arr, n):
count = 0
for i in range (n):
sum_ = arr[i]
flag = False
for j in range (i):
reqSum = 0
for k in range (j, i):
reqSum + = arr[k]
if (k - j + 1 ) > = 2 and reqSum = = sum_:
flag = True
count + = 1
break
if flag:
break
for j in range (i + 1 , n):
reqSum = 0
for k in range (j, n):
reqSum + = arr[k]
if (k - j + 1 ) > = 2 and reqSum = = sum_:
flag = True
count + = 1
break
if flag:
break
return count
arr = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 ]
n = len (arr)
print (countElements(arr, n))
|
C#
using System;
class GFG
{
public static int countElements( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int sum = arr[i];
bool flag = false ;
for ( int j = 0; j < i; j++) {
int reqSum = 0;
for ( int k = j; k < i; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
for ( int j = i + 1; j < n && flag == false ;
j++) {
int reqSum = 0;
for ( int k = j; k < n; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
}
return count;
}
public static void Main( string [] args)
{
int [] a = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
int N = a.Length;
Console.WriteLine(countElements(a, N));
}
}
|
Javascript
function countElements(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++) {
let sum = arr[i];
let flag = false ;
for (let j = 0; j < i; j++) {
let reqSum = 0;
for (let k = j; k < i; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
for (let j = i + 1; j < n && flag == false ; j++) {
let reqSum = 0;
for (let k = j; k < n; k++) {
reqSum += arr[k];
if ((k - j + 1) >= 2 && reqSum == sum) {
flag = true ;
count++;
break ;
}
}
if (flag)
break ;
}
}
return count;
}
let a = [3, 1, 4, 1, 5, 9, 2, 6, 5 ];
let N = a.length;
console.log(countElements(a, N));
|
Time Complexity: O(n3)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to optimize the above approach:
- Initialize an array cnt[] to store the number of occurrences of each array element.
- Iterate over all subarrays of at least length 2 maintaining the sum of the current subarray sum.
- If the current sum does not exceed N, then add cnt[sum] to the answer and set cnt[sum]=0 to prevent counting the same elements several times.
- Finally, print the sum obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countElements( int a[], int n)
{
int cnt[n + 1] = {0};
memset (cnt, 0, sizeof (cnt));
int ans = 0;
for ( int i = 0; i < n; i++)
{
++cnt[a[i]];
}
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)
{
ans += cnt[sum];
cnt[sum] = 0;
}
}
}
return ans;
}
int main()
{
int a[] = { 1, 1, 1, 1, 1 };
int N = sizeof (a) / sizeof (a[0]);
cout << countElements(a, N);
}
|
Java
import java.util.*;
class GFG {
static int countElements( int [] a, int n)
{
int [] cnt = new int [n + 1 ];
int ans = 0 ;
for ( int k : a) {
++cnt[k];
}
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) {
ans += cnt[sum];
cnt[sum] = 0 ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int [] a = { 1 , 1 , 1 , 1 , 1 };
System.out.println(
countElements(a, a.length));
}
}
|
Python3
def countElements(a, n):
cnt = [ 0 ] * (n + 1 )
ans = 0
for k in a:
cnt[k] + = 1
for l in range (n):
sum = 0
for r in range (l, n):
sum + = a[r]
if (l = = r):
continue
if ( sum < = n):
ans + = cnt[ sum ]
cnt[ sum ] = 0
return ans
if __name__ = = '__main__' :
a = [ 1 , 1 , 1 , 1 , 1 ]
print (countElements(a, len (a)))
|
C#
using System;
class GFG{
static int countElements( int [] a, int n)
{
int [] cnt = new int [n + 1];
int ans = 0;
foreach ( int k in a)
{
++cnt[k];
}
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)
{
ans += cnt[sum];
cnt[sum] = 0;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int [] a = { 1, 1, 1, 1, 1 };
Console.WriteLine(countElements(a, a.Length));
}
}
|
Javascript
<script>
function countElements(a, n)
{
var cnt = Array(n + 1).fill(0);
var ans = 0;
for (k = 0; k < n; k++)
{
cnt[a[k]]++;
}
for (l = 0; l < n; ++l)
{
var sum = 0;
for (r = l; r < n; ++r)
{
sum += a[r];
if (l == r)
continue ;
if (sum <= n)
{
ans += cnt[sum];
cnt[sum] = 0;
}
}
}
return ans;
}
var a = [ 1, 1, 1, 1, 1 ];
document.write(countElements(a, a.length));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...