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++
// 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 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;
}
// Driver Code
int main()
{
// Given array
int a[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
int N = sizeof(a) / sizeof(a[0]);
// Function call
cout << countElements(a, N);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Function to find the number of
// array elements that can be
// represented as the sum of two
// or more consecutive array elements
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)
{
// Given array
int a[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
int N = a.length;
// Function call
System.out.println(countElements(a, N));
}
}
// This code is contributed by sourabhdalal0001.
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
# Given array
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5]
n = len(arr)
# Function call
print(countElements(arr, n))
C#
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
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)
{
// Given array
int[] a = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
int N = a.Length;
// Function call
Console.WriteLine(countElements(a, N));
}
}
// This code is contributed by divya_p123.
JavaScript
// Javascript 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
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;
}
// Driver Code
// Given array
let a = [3, 1, 4, 1, 5, 9, 2, 6, 5 ];
let N = a.length;
// Function call
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++
// 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
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
<script>
// Javascript 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
function countElements(a, n)
{
// Stores the frequencies
// of array elements
var cnt = Array(n + 1).fill(0);
// Stores required count
var ans = 0;
// Update frequency of
// each array element
for(k = 0; k < n; k++)
{
cnt[a[k]]++;
}
// Find sum of all subarrays
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)
{
// Increment ans by cnt[sum]
ans += cnt[sum];
// Reset cnt[sum] by 0
cnt[sum] = 0;
}
}
}
// Return ans
return ans;
}
// Driver Code
// Given array
var a = [ 1, 1, 1, 1, 1 ];
// Function call
document.write(countElements(a, a.length));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
Count integers up to N which can be represented as sum of two or more consecutive numbers Given a positive integer N, the task is to count the number of integers upto N which can be represented as the sum of two or more consecutive numbers. Examples: Input: N = 7Output: 4Explanation: In the range [1, 7]: {3, 5, 6, 7} can be represented as sum of consecutive numbers. 3 = 1 + 25 = 2 + 36 =
7 min read
Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equal Given two integers M and N, the task is to find the number of N-length arrays possible having non-equal adjacent elements lying in the range [1, M] having elements at first and last indices equal. Examples: Input: N = 3, M = 3Output: 6Explanation:The possible arrays are {1, 2, 1}, {1, 3, 1}, {2, 1,
7 min read
Count of elements A[i] such that A[i] + 1 is also present in the Array Given an integer array arr the task is to count the number of elements 'A[i]', such that A[i] + 1 is also present in the array.Note: If there are duplicates in the array, count them separately.Examples: Input: arr = [1, 2, 3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.Input:
11 min read
Count of arrays having consecutive element with different values Given three positive integers n, k and x. The task is to count the number of different array that can be formed of size n such that each element is between 1 to k and two consecutive element are different. Also, the first and last elements of each array should be 1 and x respectively. Examples : Inp
10 min read
Count all distinct pairs of repeating elements from the array for every array element Given an array arr[] of N integers. For each element in the array, the task is to count the possible pairs (i, j), excluding the current element, such that i < j and arr[i] = arr[j]. Examples: Input: arr[] = {1, 1, 2, 1, 2}Output: 2 2 3 2 3Explanation:For index 1, remaining elements after excludi
7 min read
Count subarrays having a single distinct element that can be obtained from a given array Given an array arr[] of size N, the task is to count the number of subarrays consisting of a single distinct element that can be obtained from a given array. Examples: Input: N = 4, arr[] = { 2, 2, 2, 2 }Output: 7Explanation: All such subarrays {{2}, {2}, {2}, {2}, {2, 2}, {2, 2, 2}, {2, 2, 2, 2}}.
5 min read