📌  相关文章
📜  数字和等于 X 的子数组的计数

📅  最后修改于: 2022-05-13 01:56:05.910000             🧑  作者: Mango

数字和等于 X 的子数组的计数

给定一个长度为N和整数X的数组arr[] ,任务是计算数字和等于X的子数组的数量。

例子:

朴素方法:问题的朴素方法是基于检查每个子数组。对于每个子数组,检查数字总和是否等于 X 并相应地增加计数。

时间复杂度: O(N 2 * d) 其中 d 是数组元素中的最大位数
辅助空间: O(1)

有效的方法:有效的方法是使用地图。使用地图来跟踪已经获得的数字总和。跟踪当前数字总和,如果它等于X增量计数。并在地图中检查(current sum – X) 。不断更新地图中的当前数字总和。

下面是上述方法的实现。

C++
// C++ program to implement the approach
#include 
using namespace std;
 
// Function to replace the array elements
// with their digit sum value
void convertInteger(int arr[], int N)
{
    int i, sum = 0;
    for (i = 0; i < N; i++) {
        int temp = arr[i];
        while (temp) {
 
            // Store the last digit
            int l = temp % 10;
            sum += l;
            temp = temp / 10;
        }
       
        // Update the integer by
        // its digit sum
        arr[i] = sum;
        sum = 0;
    }
}
 
// Function to count number of subarrays
// having digit sum equal to X.
int CountSubarraySum(int arr[], int N, int X)
{
    // replace all the array element into
    // their digit sum value
    convertInteger(arr, N);
   
    // Map to store the digit sum
    unordered_map mp;
   
    int count = 0;
 
    // Sum of elements so far.
    int sum = 0;
 
    // Loop to calculate number of subarrays
    for (int i = 0; i < N; i++) {
 
        // Add current  array element
        // to the digit sum so far.
        sum += arr[i];
 
        // Increment count if current sum
        // equal to X
        if (sum == X)
            count++;
 
        // Find (sum - X) in the map
        if (mp.find(sum - X) != mp.end())
            count += (mp[sum - X]);
 
        // Update the map with sum for
        // count of different values of X
        mp[sum]++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 50, 30, 13, 21, 10 };
    int sum = 8;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << CountSubarraySum(arr, N, sum);
    return 0;
}


Java
// Java program to implement the approach
 
import java.util.*;
 
class GFG {
 
    // Function to replace the array elements
    // with their digit sum value
    public static void convertInteger(int arr[], int N)
    {
        int i, sum = 0;
        for (i = 0; i < N; i++) {
            int temp = arr[i];
            while (temp > 0) {
 
                // Store the last digit
                int l = temp % 10;
                sum += l;
                temp = temp / 10;
            }
 
            // Update the integer by
            // its digit sum
            arr[i] = sum;
            sum = 0;
        }
    }
 
    // Function to count number of subarrays
    // having digit sum equal to X.
    public static int CountSubarraySum(int arr[], int N,
                                       int X)
    {
        // replace all the array element into
        // their digit sum value
        convertInteger(arr, N);
 
        // Map to store the digit sum
        HashMap mp = new HashMap<>();
 
        int count = 0;
 
        // Sum of elements so far.
        int sum = 0;
 
        // Loop to calculate number of subarrays
        for (int i = 0; i < N; i++) {
 
            // Add current  array element
            // to the digit sum so far.
            sum += arr[i];
 
            // Increment count if current sum
            // equal to X
            if (sum == X)
                count++;
 
            // Find (sum - X) in the map
            if (mp.containsKey(sum - X))
                count += (mp.get(sum - X));
 
            // Update the map with sum for
            // count of different values of X
            if (mp.containsKey(sum))
                mp.put(sum, mp.get(sum) + 1);
            else
                mp.put(sum, 1);
        }
 
        return count;
    }
 
    // driver code
 
    public static void main(String[] args)
    {
 
        int arr[] = { 50, 30, 13, 21, 10 };
        int sum = 8;
        int N = arr.length;
 
        System.out.println(CountSubarraySum(arr, N, sum));
    }
}
 
// This code is contributed by Palak Gupta


Python3
# Python code for the above approach
 
# Function to replace the array elements
# with their digit sum value
def convertInteger(arr, N):
    sum = 0
    for i in range(N):
        temp = arr[i]
        while (temp):
 
            # Store the last digit
            l = temp % 10
            sum += l
            temp = (temp // 10)
 
        # Update the integer by
        # its digit sum
        arr[i] = sum
        sum = 0
 
# Function to count number of subarrays
# having digit sum equal to X.
def CountSubarraySum(arr, N, X):
 
    # replace all the array element into
    # their digit sum value
    convertInteger(arr, N)
 
    # Map to store the digit sum
    mp = {}
 
    count = 0
 
    # Sum of elements so far.
    sum = 0
 
    # Loop to calculate number of subarrays
    for i in range(N):
 
        # Add current  array element
        # to the digit sum so far.
        sum += arr[i]
 
        # Increment count if current sum
        # equal to X
        if (sum == X):
            count += 1
 
        # Find (sum - X) in the map
        if ((sum - X) in mp):
            count += (mp[(sum - X)])
 
        # Update the map with sum for
        # count of different values of X
        if (sum in mp):
            mp[sum] += 1
        else:
            mp[sum] = 1
 
    return count
 
# Driver code
arr = [50, 30, 13, 21, 10]
sum = 8
N = len(arr)
 
print(CountSubarraySum(arr, N, sum))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to replace the array elements
  // with their digit sum value
  static void convertInteger(int[] arr, int N)
  {
    int i, sum = 0;
    for (i = 0; i < N; i++) {
      int temp = arr[i];
      while (temp != 0) {
 
        // Store the last digit
        int l = temp % 10;
        sum += l;
        temp = temp / 10;
      }
 
      // Update the integer by
      // its digit sum
      arr[i] = sum;
      sum = 0;
    }
  }
 
  // Function to count number of subarrays
  // having digit sum equal to X.
  static int CountSubarraySum(int[] arr, int N, int X)
  {
    // replace all the array element into
    // their digit sum value
    convertInteger(arr, N);
 
    // Map to store the digit sum
    Dictionary mp = new Dictionary();
 
    int count = 0;
 
    // Sum of elements so far.
    int sum = 0;
 
    // Loop to calculate number of subarrays
    for (int i = 0; i < N; i++) {
 
      // Add current  array element
      // to the digit sum so far.
      sum += arr[i];
 
      // Increment count if current sum
      // equal to X
      if (sum == X)
        count++;
 
      // Find (sum - X) in the map
      if (mp.ContainsKey(sum - X))
        count += (mp[sum - X]);
 
      // Update the map with sum for
      // count of different values of X
      if(mp.ContainsKey(sum))
      {
        mp[sum] = i;
      }
      else{
        mp.Add(sum, i);
      }
    }
 
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 50, 30, 13, 21, 10 };
    int sum = 8;
    int N = arr.Length;
 
    Console.Write(CountSubarraySum(arr, N, sum));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript



输出
2

时间复杂度: O(N * d) 其中 d 是数组元素中的最大位数
辅助空间: O(N)