给定一个数组arr[] ,任务是对数组中的元素进行计数,使得存在一个其总和等于该元素的子数组。
注意:子数组的长度必须大于 1。
例子:
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
方法:想法是将数组元素的频率存储在哈希映射中。然后,迭代每个可能的子数组并检查其总和是否存在于哈希映射中。如果是,则通过它们的频率增加这些元素的计数。
下面是上述方法的实现:
C++
// C++ implementation to count the
// elements such that their exist
// a subarray whose sum is equal to
// this element
#include
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 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
输出:
4
时间复杂度: O(N 2 )
空间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。