Count of elements which is the sum of a subarray of the given Array
Last Updated :
09 Mar, 2023
Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.
Note: Length of subarray must be greater than 1.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 4
Explanation:
There are 4 such elements in array -
arr[2] = 3 => arr[0-1] => 1 + 2 = 3
arr[4] = 5 => arr[1-2] => 2 + 3 = 5
arr[5] = 6 => arr[0-2] => 1 + 2 + 3 = 6
arr[6] = 7 => arr[2-3] => 3 + 4 = 7
Input: arr[] = {1, 2, 3, 3}
Output: 2
There are 2 such elements in array -
arr[2] = 3 => arr[0-1] => 1 + 2 = 3
arr[3] = 3 => arr[0-1] => 1 + 2 = 3
Approach: The idea is to store the frequency of the elements of the array in a hash-map. Then, iterate over every possible subarray and check that its sum is present in the hash-map. If yes, then increment the count of such elements by their frequency.
Algorithm:
- Start the function countElement with the given array 'arr' and its length 'n'.
- Create a map named 'freq' to store the frequency of each element in the array.
- Initialize a variable named 'ans' to 0.
- Loop through the array 'arr' and increment the frequency of each element in the 'freq' map.
- Loop through the array again, starting from the first index and going up to the second last index.
- Within the previous loop, initialize a variable named 'tmpsum' to the value of the current array element.
- Loop through the array again, starting from the next index and going up to the last index.
- Within the previous loop, add the current array element to 'tmpsum'.
- Check if 'tmpsum' exists as a key in the 'freq' map. If it does, increment 'ans' by the value corresponding to that key.
- Return the value of 'ans'.
- End.
Pseudocode:
countElement(arr, n):
freq = create a new empty map
ans = 0
for i from 0 to n-1:
freq[arr[i]] += 1
for i from 0 to n-2:
tmpsum = arr[i]
for j from i+1 to n-1:
tmpsum += arr[j]
if tmpsum is a key in freq:
ans += freq[tmpsum]
return ans
main:
arr = create a new integer array with some values
n = calculate the size of arr
result = countElement(arr, n)
print result
Below is the implementation of the above approach:
C++
// C++ implementation to count the
// elements such that their exist
// a subarray whose sum is equal to
// this element
#include <bits/stdc++.h>
using namespace std;
// Function to count the elements
// such that their exist a subarray
// whose sum is equal to this element
int countElement(int arr[], int n)
{
map<int, int> freq;
int ans = 0;
// Loop to count the frequency
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
}
// Loop to iterate over every possible
// subarray of the array
for (int i = 0; i < n - 1; i++) {
int tmpsum = arr[i];
for (int j = i + 1; j < n; j++) {
tmpsum += arr[j];
if (freq.find(tmpsum) != freq.end()) {
ans += freq[tmpsum];
}
}
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countElement(arr, n) << endl;
return 0;
}
Java
// Java implementation to count the
// elements such that their exist
// a subarray whose sum is equal to
// this element
import java.util.*;
class GFG {
// Function to count the elements
// such that their exist a subarray
// whose sum is equal to this element
static int countElement(int arr[], int n)
{
int freq[] = new int[n + 1];
int ans = 0;
// Loop to count the frequency
for(int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
// Loop to iterate over every possible
// subarray of the array
for(int i = 0; i < n - 1; i++)
{
int tmpsum = arr[i];
for(int j = i + 1; j < n; j++)
{
tmpsum += arr[j];
if (tmpsum <= n)
{
ans += freq[tmpsum];
freq[tmpsum] = 0;
}
}
}
return ans;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = arr.length;
System.out.println(countElement(arr, n));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 implementation to count the
# elements such that their exist
# a subarray whose sum is equal to
# this element
# Function to count element such
# that their exist a subarray whose
# sum is equal to this element
def countElement(arr, n):
freq = {}
ans = 0
# Loop to compute frequency
# of the given elements
for i in range(n):
freq[arr[i]] = \
freq.get(arr[i], 0) + 1
# Loop to iterate over every
# possible subarray of array
for i in range(n-1):
tmpsum = arr[i]
for j in range(i + 1, n):
tmpsum += arr[j]
if tmpsum in freq:
ans += freq[tmpsum]
return ans
# Driver Code
if __name__ == "__main__":
arr =[1, 2, 3, 4, 5, 6, 7]
n = len(arr)
print(countElement(arr, n))
C#
// C# implementation to count the
// elements such that their exist
// a subarray whose sum is equal to
// this element
using System;
class GFG {
// Function to count the elements
// such that their exist a subarray
// whose sum is equal to this element
static int countElement(int[] arr, int n)
{
int[] freq = new int[n + 1];
int ans = 0;
// Loop to count the frequency
for(int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
// Loop to iterate over every possible
// subarray of the array
for(int i = 0; i < n - 1; i++)
{
int tmpsum = arr[i];
for(int j = i + 1; j < n; j++)
{
tmpsum += arr[j];
if (tmpsum <= n)
{
ans += freq[tmpsum];
freq[tmpsum] = 0;
}
}
}
return ans;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
int n = arr.Length;
Console.WriteLine(countElement(arr, n));
}
}
// This code is contributed by AbhiThakur
JavaScript
<script>
// Javascript implementation to count the
// elements such that their exist
// a subarray whose sum is equal to
// this element
// Function to count the elements
// such that their exist a subarray
// whose sum is equal to this element
function countElement(arr, n)
{
let freq = Array.from({length: n+1}, (_, i) => 0);
let ans = 0;
// Loop to count the frequency
for(let i = 0; i < n; i++)
{
freq[arr[i]]++;
}
// Loop to iterate over every possible
// subarray of the array
for(let i = 0; i < n - 1; i++)
{
let tmpsum = arr[i];
for(let j = i + 1; j < n; j++)
{
tmpsum += arr[j];
if (tmpsum <= n)
{
ans += freq[tmpsum];
freq[tmpsum] = 0;
}
}
}
return ans;
}
// Driver Code
let arr = [ 1, 2, 3, 4, 5, 6, 7 ];
let n = arr.length;
document.write(countElement(arr, n));
</script>
Time Complexity: O(N2)
Space Complexity: O(N)
Similar Reads
Count subarrays with non-zero sum in the given Array Given an array arr[] of size N, the task is to count the total number of subarrays for the given array arr[] which have a non-zero-sum.Examples: Input: arr[] = {-2, 2, -3} Output: 4 Explanation: The subarrays with non zero sum are: [-2], [2], [2, -3], [-3]. All possible subarray of the given input a
7 min read
Find sum of count of duplicate numbers in all subarrays of given array Given an array arr[] of size N. The task it to find the sum of count of duplicate numbers in all subarrays of given array arr[]. For example array {1, 2, 3, 2, 3, 2} has two duplicate elements (i.e, 2 and 3 come more than one time in the array). Examples:Input: N = 2, arr = {3,3}Output: 1Explanation
6 min read
Count of Subarrays with sum equals k in given Binary Array Given a binary array arr[] and an integer k, the task is to find the count of non-empty subarrays with a sum equal to k.Examples:Input: arr[] = {1, 0, 1, 1, 0, 1}, k = 2Output: 6Explanation: All valid subarrays are: {1, 0, 1}, {0, 1, 1}, {1, 1}, {1, 0, 1}, {0, 1, 1, 0}, {1, 1, 0}.Input: arr[] = {0,
10 min read
Check if a subarray exists with sum greater than the given Array Given an array of integers arr, the task is to check if there is a subarray (except the given array) such that the sum of its elements is greater than or equal to the sum of elements of the given array. If no such subarray is possible, print No, else print Yes.Examples: Input: arr = {5, 6, 7, 8} Out
7 min read
Count of Subarrays which Contain the Length of that Subarray Given an array A[] of length N, the task is to count the number of subarrays of A[] that contain the length of that subarray. Examples: Input: A = {10, 11, 1}, N = 3Output: 1Explanation: Only the subarray {1}, with a length 1, contains its own length. Input: A = [1, 2, 3, 4, 5], N = 5Output: 9Explan
8 min read
Count of subarrays which start and end with the same element Given an array A of size N where the array elements contain values from 1 to N with duplicates, the task is to find the total number of subarrays that start and end with the same element. Examples: Input: A[] = {1, 2, 1, 5, 2} Output: 7 Explanation: Total 7 sub-array of the given array are {1}, {2},
10 min read
Find array elements equal to sum of any subarray of at least size 2 Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
6 min read
Count sub-arrays which have elements less than or equal to X Given an array of n elements and an integer X. Count the number of sub-arrays of this array which have all elements less than or equal to X. Examples: Input : arr[] = {1, 5, 7, 8, 2, 3, 9} X = 6 Output : 6 Explanation : Sub-arrays are {1}, {5}, {2}, {3}, {1, 5}, {2, 3} Input : arr[] = {1, 10, 12, 4,
15 min read
Count subarrays with equal number of occurrences of two given elements Given an array and two integers say, x and y, find the number of subarrays in which the number of occurrences of x is equal to the number of occurrences of y. Examples: Input : arr[] = {1, 2, 1}, x = 1, y = 2 Output : 2 The possible sub-arrays have same equal number of occurrences of x and y are: 1)
14 min read
Count of subsequences in an array with sum less than or equal to X Given an integer array arr[] of size N and an integer X, the task is to count the number of subsequences in that array such that its sum is less than or equal to X. Note: 1 <= N <= 1000 and 1 <= X <= 1000, where N is the size of the array. Examples: Input : arr[] = {84, 87, 73}, X = 100
13 min read