📜  不包含总和为0的任何子数组的子数组的计数

📅  最后修改于: 2021-05-19 18:58:33             🧑  作者: Mango

给定数组arr ,任务是查找给定数组的子数组总数,该子数组不包含元素和为零的任何子数组。

例子:

方法:

  1. 首先,将数组的所有元素存储为先前元素的总和。
  2. 现在,使用两个指针,增加第二个指针,并将值存储在映射中,而不会遇到相同的元素。
  3. 如果遇到映射中已经存在的元素,则意味着两个指针之间存在一个子数组,其元素和等于0。
  4. 现在增加第一个指针,并在存在两个相同元素的同时从map中删除该元素。
  5. 将答案存储在变量中,最后将其返回。

下面是上述方法的实现:

C++
// C++ program to Count the no of subarray
// which do not contain any subarray
// whose sum of elements is equal to zero
  
#include 
using namespace std;
  
// Function that print the number of
// subarrays which do not contain any subarray
// whose elements sum is equal to 0
void numberOfSubarrays(int arr[], int n)
{
    vector v(n + 1);
    v[0] = 0;
  
    // Storing each element as sum
    // of its previous element
    for (int i = 0; i < n; i++) {
        v[i + 1] = v[i] + arr[i];
    }
  
    map mp;
  
    int begin = 0, end = 0, answer = 0;
  
    mp[0] = 1;
  
    while (begin < n) {
  
        while (end < n
               && mp.find(v[end + 1])
                      == mp.end()) {
            end++;
            mp[v[end]] = 1;
        }
  
        // Check if another same element found
        // this means a subarray exist between
        // end and begin whose sum
        // of elements is equal to 0
        answer = answer + end - begin;
  
        // Erase beginning element from map
        mp.erase(v[begin]);
  
        // Increase begin
        begin++;
    }
  
    // Print the result
    cout << answer << endl;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 2, 4, -6 };
    int size = sizeof(arr) / sizeof(arr[0]);
  
    numberOfSubarrays(arr, size);
  
    return 0;
}


Java
// Java program to Count the no of subarray
// which do not contain any subarray
// whose sum of elements is equal to zero
import java.util.*;
  
class GFG{
   
// Function that print the number of
// subarrays which do not contain any subarray
// whose elements sum is equal to 0
static void numberOfSubarrays(int arr[], int n)
{
    int []v = new int[n + 1];
    v[0] = 0;
   
    // Storing each element as sum
    // of its previous element
    for (int i = 0; i < n; i++) {
        v[i + 1] = v[i] + arr[i];
    }
   
    HashMap mp = new HashMap();
   
    int begin = 0, end = 0, answer = 0;
   
    mp.put(0, 1);
   
    while (begin < n) {
   
        while (end < n
               && !mp.containsKey(v[end + 1])) {
            end++;
            mp.put(v[end],  1);
        }
   
        // Check if another same element found
        // this means a subarray exist between
        // end and begin whose sum
        // of elements is equal to 0
        answer = answer + end - begin;
   
        // Erase beginning element from map
        mp.remove(v[begin]);
   
        // Increase begin
        begin++;
    }
   
    // Print the result
    System.out.print(answer +"\n");
}
   
// Driver Code
public static void main(String[] args)
{
   
    int arr[] = { 2, 4, -6 };
    int size = arr.length;
   
    numberOfSubarrays(arr, size);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python 3 program to Count the no of subarray
# which do not contain any subarray
# whose sum of elements is equal to zero
  
# Function that print the number of
# subarrays which do not contain any subarray
# whose elements sum is equal to 0
def numberOfSubarrays(arr, n):
  
    v = [0]*(n + 1)
  
    # Storing each element as sum
    # of its previous element
    for i in range( n):
        v[i + 1] = v[i] + arr[i]
  
    mp = {}
  
    begin, end, answer = 0 , 0 , 0
  
    mp[0] = 1
  
    while (begin < n):
  
        while (end < n
            and (v[end + 1]) not in mp):
            end += 1
            mp[v[end]] = 1
  
        # Check if another same element found
        # this means a subarray exist between
        # end and begin whose sum
        # of elements is equal to 0
        answer = answer + end - begin
  
        # Erase beginning element from map
        del mp[v[begin]]
  
        # Increase begin
        begin += 1
  
    # Print the result
    print(answer)
  
# Driver Code
if __name__ == "__main__":
      
    arr = [ 2, 4, -6 ]
    size = len(arr)
    numberOfSubarrays(arr, size)
  
# This code is contributed by chitranayal


C#
// C# program to Count the no of subarray
// which do not contain any subarray
// whose sum of elements is equal to zero
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function that print the number of
// subarrays which do not contain any subarray
// whose elements sum is equal to 0
static void numberOfSubarrays(int []arr, int n)
{
    int []v = new int[n + 1];
    v[0] = 0;
    
    // Storing each element as sum
    // of its previous element
    for (int i = 0; i < n; i++) {
        v[i + 1] = v[i] + arr[i];
    }
    
    Dictionary mp = new Dictionary();
    
    int begin = 0, end = 0, answer = 0;
    
    mp.Add(0, 1);
    
    while (begin < n) {
    
        while (end < n
               && !mp.ContainsKey(v[end + 1])) {
            end++;
            mp.Add(v[end],  1);
        }
    
        // Check if another same element found
        // this means a subarray exist between
        // end and begin whose sum
        // of elements is equal to 0
        answer = answer + end - begin;
    
        // Erase beginning element from map
        mp.Remove(v[begin]);
    
        // Increase begin
        begin++;
    }
    
    // Print the result
    Console.Write(answer +"\n");
}
    
// Driver Code
public static void Main(String[] args)
{
    
    int []arr = { 2, 4, -6 };
    int size = arr.Length;
    
    numberOfSubarrays(arr, size);
}
}
  
// This code is contributed by Rajput-Ji


输出:
5