Given an array arr[] and integer K, the task is to count the number of subsequences of the array such that after adding all the elements of that subsequences their single digit integer sum is exactly K.
Note: Single digit integer sum is obtained by replacing a number with its digit sum until the number is a single digit. e.g. 58 becomes 13 and then 4. 4 is the single digit sum of 53.
Examples:
Input: arr[] = {9, 8, 6, 10}, K =6
Output: 4
Explanation: The example has 15 subsequences: {9}, {9, 8}, { 9, 8, 6}, {9, 8, 6, 10}, {9, 6}, {9, 6, 10},
{9, 8, 10}, {9, 10}, {8}, {8, 6}, {8, 6, 10}, {8, 10}, {6}, {6, 10}, {10} .
Now their sums are 9, 17, 23, 33, 15, 25, 27, 19, 8, 14, 24, 18, 6, 16, 10 respectively.
Their single digit integer sums are 9, 8, 5, 6, 6, 7, 9, 1, 8, 5, 6, 9, 6, 7, 1 respectively.
Here 6 is repeating 4 times. So 4 is the answer.Input: arr[] = {9, 8, 6, 10}, K =7
Output: 2
Approach: The idea to solve the problem is to find all the subsequences of the array and the for each subsequence check if the single digit integer sum of the subsequence sum is K.
Follow the steps mentioned below to solve the problem:
- Find all the subsequences of the array.
- For each subsequence:
- Find the sum of the subsequence.
- Calculate the single digit integer sum (say X) of the subsequence sum.
- Check if X is equal to K and increase the count if they are equal.
- Return the final count as the required answer.
Below is the implementation of the above approach.
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std;
// Function to replace integers // with their digit sum value int convert( int X)
{ int sum = 0, temp = X;
// Run a while till digit sum
// become single integer
while (temp > 9) {
int temp2 = temp;
while (temp2) {
// Store the last digit
int l = temp2 % 10;
sum += l;
temp2 = temp2 / 10;
}
temp = sum;
sum = 0;
}
// Return digit sum
return temp;
} // Function to count subsequences void countSubsequences(vector< int > arr, int i,
vector< int > s, int & count, int K)
{ // If it is the leaf of
// recursion tree
if (i == arr.size()) {
// Calculating subsequences
// sum using stl
int subSum = accumulate(s.begin(), s.end(), 0);
// First convert subsequences sum
// into single integer digit sum and
// check if it equal to K or not if
// it is then increment count
if (convert(subSum) == K)
count++;
}
else {
// Subsequence without including
// the element at current index
countSubsequences(arr, i + 1, s,
count, K);
// Push back current element
s.push_back(arr[i]);
// Subsequence including
// the element at current index
countSubsequences(arr, i + 1, s,
count, K);
}
return ;
} // Function to find all the subsequences int solve(vector< int >& arr, int K)
{ int count = 0, i = 0;
vector< int > v;
// Recursive function call
countSubsequences(arr, i, v, count, K);
return count;
} // Driver code int main()
{ vector< int > arr = { 9, 8, 6, 10 };
int K = 6;
int count = solve(arr, K);
cout << count;
return 0;
} |
// Java implementation of above approach import java.io.*;
import java.util.*;
class GFG
{ // Function to replace integers
// with their digit sum value
public static int convert( int X)
{
int sum = 0 , temp = X;
// Run a while till digit sum
// become single integer
while (temp > 9 ) {
int temp2 = temp;
while (temp2 > 0 ) {
// Store the last digit
int l = temp2 % 10 ;
sum += l;
temp2 = temp2 / 10 ;
}
temp = sum;
sum = 0 ;
}
// Return digit sum
return temp;
}
// Function to count subsequences
public static void
countSubsequences( int arr[], int i,
ArrayList<Integer> s, int count[],
int K)
{
// If it is the leaf of
// recursion tree
if (i == arr.length) {
// Calculating subsequences
// sum using stl
int subSum = 0 ;
for ( int j = 0 ; j < s.size(); j++)
subSum += s.get(j);
// First convert subsequences sum
// into single integer digit sum and
// check if it equal to K or not if
// it is then increment count
if (convert(subSum) == K)
count[ 0 ]++;
}
else {
// Subsequence without including
// the element at current index
countSubsequences(arr, i + 1 , s, count, K);
// Push back current element
s.add(arr[i]);
// Subsequence including
// the element at current index
countSubsequences(arr, i + 1 , s, count, K);
s.remove(s.size()- 1 );
}
return ;
}
// Function to find all the subsequences
public static int [] solve( int arr[], int K)
{
int count[] = { 0 };
int i = 0 ;
ArrayList<Integer> list = new ArrayList<>();
// Recursive function call
countSubsequences(arr, i, list, count, K);
return count;
}
public static void main(String[] args)
{
int arr[] = { 9 , 8 , 6 , 10 };
int K = 6 ;
int count[] = solve(arr, K);
System.out.print(count[ 0 ]);
}
} // This code is contributed by Rohit Pradhan |
# Python3 code to implement the above approach # function to convert integer # to its digit sum value def convert(X):
sums = 0
temp = X
while temp > 9 :
temp2 = temp
while temp2 > 0 :
sums + = temp2 % 10
temp2 / / = 10
temp = sums
sums = 0
return temp
# function to count subsequence def countSubsequences(arr, i, s, K):
global count
if i = = len (arr):
if convert( sum (s)) = = K:
count + = 1
else :
countSubsequences(arr, i + 1 , s, K)
countSubsequences(arr, i + 1 , s + [arr[i]], K)
return
def solve(K):
global count
count = 0
i = 0
v = []
countSubsequences(arr, 0 , [], K)
return count
# Driver Code arr = [ 9 , 8 , 6 , 10 ]
K = 6
print (solve(K))
# This code is contributed by phasing17. |
// C# implementation of above approach using System;
using System.Collections.Generic;
public class GFG
{ // Function to replace integers
// with their digit sum value
public static int convert( int X)
{
int sum = 0, temp = X;
// Run a while till digit sum
// become single integer
while (temp > 9) {
int temp2 = temp;
while (temp2 > 0) {
// Store the last digit
int l = temp2 % 10;
sum += l;
temp2 = temp2 / 10;
}
temp = sum;
sum = 0;
}
// Return digit sum
return temp;
}
// Function to count subsequences
public static void
countSubsequences( int [] arr, int i,
List< int > s, int [] count,
int K)
{
// If it is the leaf of
// recursion tree
if (i == arr.Length) {
// Calculating subsequences
// sum using stl
int subSum = 0;
for ( int j = 0; j < s.Count; j++)
subSum += s[j];
// First convert subsequences sum
// into single integer digit sum and
// check if it equal to K or not if
// it is then increment count
if (convert(subSum) == K)
count[0]++;
}
else {
// Subsequence without including
// the element at current index
countSubsequences(arr, i + 1, s, count, K);
// Push back current element
s.Add(arr[i]);
// Subsequence including
// the element at current index
countSubsequences(arr, i + 1, s, count, K);
s.RemoveAt(s.Count-1);
}
return ;
}
// Function to find all the subsequences
public static int [] solve( int [] arr, int K)
{
int [] count = {0};
int i = 0;
List< int > list = new List< int >();
// Recursive function call
countSubsequences(arr, i, list, count, K);
return count;
}
public static void Main( string [] args)
{
int [] arr = { 9, 8, 6, 10 };
int K = 6;
int [] count = solve(arr, K);
Console.WriteLine(count[0]);
}
} // This code is contributed by phasing17 |
<script> // JavaScript program for the above approach // Function to replace integers // with their digit sum value let count = 0; function convert(X)
{ let sum = 0, temp = X;
// Run a while till digit sum
// become single integer
while (temp > 9) {
let temp2 = temp;
while (temp2) {
// Store the last digit
let l = temp2 % 10;
sum += l;
temp2 = Math.floor(temp2 / 10);
}
temp = sum;
sum = 0;
}
// Return digit sum
return temp;
} // Function to count subsequences function countSubsequences(arr,i,s,K)
{ // If it is the leaf of
// recursion tree
if (i == arr.length) {
// Calculating subsequences
// sum using stl
let subSum = s.reduce((a, b) => a + b, 0);
// First convert subsequences sum
// into single integer digit sum and
// check if it equal to K or not if
// it is then increment count
if (convert(subSum) == K){
count += 1
}
}
else {
// Subsequence without including
// the element at current index
countSubsequences(arr, i + 1, s,K);
// Push back current element
s.push(arr[i]);
// Subsequence including
// the element at current index
countSubsequences(arr, i + 1, s,K);
s.pop();
}
return ;
} // Function to find all the subsequences function solve(arr,K)
{ let i = 0;
let v = [];
// Recursive function call
countSubsequences(arr, i, v,K);
return count;
} // Driver code let arr = [ 9, 8, 6, 10 ]; let K = 6; count = solve(arr, K); document.write(count); // This code is contributed by shinjanpatra </script> |
4
Time Complexity: O( 2N * N)
Auxiliary Space: O(N)