Count subsequences of Array having single digit integer sum K
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++
#include <bits/stdc++.h>
using namespace std;
int convert( int X)
{
int sum = 0, temp = X;
while (temp > 9) {
int temp2 = temp;
while (temp2) {
int l = temp2 % 10;
sum += l;
temp2 = temp2 / 10;
}
temp = sum;
sum = 0;
}
return temp;
}
void countSubsequences(vector< int > arr, int i,
vector< int > s, int & count, int K)
{
if (i == arr.size()) {
int subSum = accumulate(s.begin(), s.end(), 0);
if (convert(subSum) == K)
count++;
}
else {
countSubsequences(arr, i + 1, s,
count, K);
s.push_back(arr[i]);
countSubsequences(arr, i + 1, s,
count, K);
}
return ;
}
int solve(vector< int >& arr, int K)
{
int count = 0, i = 0;
vector< int > v;
countSubsequences(arr, i, v, count, K);
return count;
}
int main()
{
vector< int > arr = { 9, 8, 6, 10 };
int K = 6;
int count = solve(arr, K);
cout << count;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int convert( int X)
{
int sum = 0 , temp = X;
while (temp > 9 ) {
int temp2 = temp;
while (temp2 > 0 ) {
int l = temp2 % 10 ;
sum += l;
temp2 = temp2 / 10 ;
}
temp = sum;
sum = 0 ;
}
return temp;
}
public static void
countSubsequences( int arr[], int i,
ArrayList<Integer> s, int count[],
int K)
{
if (i == arr.length) {
int subSum = 0 ;
for ( int j = 0 ; j < s.size(); j++)
subSum += s.get(j);
if (convert(subSum) == K)
count[ 0 ]++;
}
else {
countSubsequences(arr, i + 1 , s, count, K);
s.add(arr[i]);
countSubsequences(arr, i + 1 , s, count, K);
s.remove(s.size()- 1 );
}
return ;
}
public static int [] solve( int arr[], int K)
{
int count[] = { 0 };
int i = 0 ;
ArrayList<Integer> list = new ArrayList<>();
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 ]);
}
}
|
Python3
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
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
arr = [ 9 , 8 , 6 , 10 ]
K = 6
print (solve(K))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int convert( int X)
{
int sum = 0, temp = X;
while (temp > 9) {
int temp2 = temp;
while (temp2 > 0) {
int l = temp2 % 10;
sum += l;
temp2 = temp2 / 10;
}
temp = sum;
sum = 0;
}
return temp;
}
public static void
countSubsequences( int [] arr, int i,
List< int > s, int [] count,
int K)
{
if (i == arr.Length) {
int subSum = 0;
for ( int j = 0; j < s.Count; j++)
subSum += s[j];
if (convert(subSum) == K)
count[0]++;
}
else {
countSubsequences(arr, i + 1, s, count, K);
s.Add(arr[i]);
countSubsequences(arr, i + 1, s, count, K);
s.RemoveAt(s.Count-1);
}
return ;
}
public static int [] solve( int [] arr, int K)
{
int [] count = {0};
int i = 0;
List< int > list = new List< int >();
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]);
}
}
|
Javascript
<script>
let count = 0;
function convert(X)
{
let sum = 0, temp = X;
while (temp > 9) {
let temp2 = temp;
while (temp2) {
let l = temp2 % 10;
sum += l;
temp2 = Math.floor(temp2 / 10);
}
temp = sum;
sum = 0;
}
return temp;
}
function countSubsequences(arr,i,s,K)
{
if (i == arr.length) {
let subSum = s.reduce((a, b) => a + b, 0);
if (convert(subSum) == K){
count += 1
}
}
else {
countSubsequences(arr, i + 1, s,K);
s.push(arr[i]);
countSubsequences(arr, i + 1, s,K);
s.pop();
}
return ;
}
function solve(arr,K)
{
let i = 0;
let v = [];
countSubsequences(arr, i, v,K);
return count;
}
let arr = [ 9, 8, 6, 10 ];
let K = 6;
count = solve(arr, K);
document.write(count);
</script>
|
Time Complexity: O( 2N * N)
Auxiliary Space: O(N)
Last Updated :
26 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...