Count of subarrays with digit sum equals to X
Last Updated :
25 Feb, 2022
Given an array arr[] of length N and integer X, the task is to count no of subarrays having digit sum equal to X.
Examples:
Input: arr[] = {10, 5, 13, 20, 9}, X = 6
Output: 2
Explanation: There are two subarrays which is having digit sum equal to 6.
{10, 5} => (1 + 0) + 5 = 6 and {13 , 20} => (1 + 3) + (2 + 0) = 6.
Input: arr[] = {50, 30, 13, 21, 10}, X = 8
Output: 2
Explanation: There are two subarrays which is having digit sum equal to 8.
{50, 30} => (5+0) + (3+0) = 8 and {13, 21, 10} => (1+3) + (2+1) + (1+0) = 8.
Naive Approach: The naive approach of the problem is based on checking every subarray. For every subarray check if has digit sum equal to X or not and increase the count accordingly.
Time Complexity: O(N2 * d) where d is the maximum number of digits in an array element
Auxiliary Space: O(1)
Efficient Approach: The efficient approach would be by using a map. Use the map to keep track of the digit sums already obtained. Keep track current digit sum and if it is equal to X increment count. And also check in the map for (current sum - X). Keep updating the current digit sum in the map.
Below is the implementation of the above approach.
C++
// C++ program to implement the approach
#include <bits/stdc++.h>
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<int, int> 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<Integer, Integer> 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<int,
int> mp = new Dictionary<int, int>();
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
<script>
// JavaScript code for the above approach
// Function to replace the array elements
// with their digit sum value
function convertInteger(arr, N) {
let i, sum = 0;
for (i = 0; i < N; i++) {
let temp = arr[i];
while (temp) {
// Store the last digit
let l = temp % 10;
sum += l;
temp = Math.floor(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.
function CountSubarraySum(arr, N, X)
{
// replace all the array element into
// their digit sum value
convertInteger(arr, N);
// Map to store the digit sum
let mp = new Map();
let count = 0;
// Sum of elements so far.
let sum = 0;
// Loop to calculate number of subarrays
for (let 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.has(sum - X))
count += (mp.get(sum - X));
// Update the map with sum for
// count of different values of X
if (mp.has(sum)) {
mp.set(sum, mp.get(sum) + 1)
}
else {
mp.set(sum, 1);
}
}
return count;
}
// Driver code
let arr = [50, 30, 13, 21, 10];
let sum = 8;
let N = arr.length;
document.write(CountSubarraySum(arr, N, sum));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N * d) where d is the maximum number of digits in an array element
Auxiliary Space: O(N)
Similar Reads
Count subarrays with sum equal to its XOR value Given an array arr[] containing N elements, the task is to count the number of sub-arrays whose XOR of all the elements is equal to the sum of all the elements in the subarray. Examples: Input: arr[] = {2, 5, 4, 6} Output: 5 Explanation: All the subarrays {{2}, {5}, {4}, {6}} satisfies the above con
10 min read
Count of subarrays with sum at least K Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
6 min read
Count Subarrays with given XOR Given an array of integers arr[] and a number k, the task is to count the number of subarrays having XOR of their elements as k.Examples: Input: arr[] = [4, 2, 2, 6, 4], k = 6Output: 4Explanation: The subarrays having XOR of their elements as 6 are [4, 2], [4, 2, 2, 6, 4], [2, 2, 6], and [6].Input:
10 min read
Count of subarrays having sum equal to its length Given an array arr[] of size N, the task is to find the number of subarrays having the sum of its elements equal to the number of elements in it. Examples: Input: N = 3, arr[] = {1, 0, 2}Output: 3Explanation:Total number of subarrays are 6 i.e., {1}, {0}, {2}, {1, 0}, {0, 2}, {1, 0, 2}.Out of 6 only
7 min read
Count of Subarray with B times Sum equal to C times Length Given, an array A[] of N integers, and also given two positive integers B and C, the task is to find the number of subarrays, such that B* Sum of elements of the subarray = C * Length of the subarray (here * describes multiplication). Examples: Input: N = 3, A[] = {3, -1, 1}, B = 1, C = 1Output: 3Ex
7 min read