Sum of all subsequences of a number - GeeksforGeeks

06 May, 2021
Given a number as string s, find the sum of all the elements present in all possible subsequences of s.Examples :  
Input : s = "123" 
Output : 24 
Explanation : all possible sub-sequences are 
1, 2, 3, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3} 
which add up to 24 

Input : s = "453" 
Output : 48 
 
Approach : Using power set, we can find all the subsequences and sum up all the subsequences individually in a different function. The total number of subsequences possible are 2n-1. Add the sum of all subsequences to the combined sum which is the final output. Below is the implementation of above approach.  
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find the sum of// elements present in all subsequences#include <bits/stdc++.h>using namespace std; // Returns numeric value of a subsequence of// s. The subsequence to be picked is decided// using bit pattern of num (We pick all those// digits for which there is a set bit in num)int findSubSequence(string s, int num){     // Initialize the result    int res = 0;     // till n!=0    int i = 0;    while (num) {                 // if i-th bit is set        // then add this number        if (num & 1)            res += s[i] - '0';        i++;                 // right shift i        num = num >> 1;    }     return res;} // function to find combined sum// of all individual subsequence sumint combinedSum(string s){    // length of string    int n = s.length();         // stores the combined    int c_sum = 0;     // 2^n-1 subsequences    int range = (1 << n) - 1;     // loop for all subsequences    for (int i = 0; i <= range; i++)        c_sum += findSubSequence(s, i);     // returns the combined sum    return c_sum;} // driver codeint main(){    string s = "123";    cout << combinedSum(s);       return 0;}
// Java program to find the sum of elements// present in all subsequencesimport java.io.*; class GFG {         // Returns numeric value of a    // subsequence of s. The subsequence    // to be picked is decided using bit    // pattern of num (We pick all those    // digits for which there is a set    // bit in num)    static int findSubSequence(String s,                                int num)    {        // Initialize the result        int res = 0;             // till n!=0        int i = 0;        while (num > 0) {                         // if i-th bit is set            // then add this number            if ((num & 1) == 1)                res += s.charAt(i) - '0';            i++;                         // right shift i            num = num >> 1;        }             return res;    }     // function to find combined sum    // of all individual subsequence    // sum    static int combinedSum(String s)    {                 // length of String        int n = s.length();                 // stores the combined        int c_sum = 0;             // 2^n-1 subsequences        int range = (1 << n) - 1;             // loop for all subsequences        for (int i = 0; i <= range; i++)            c_sum += findSubSequence(s, i);             // returns the combined sum        return c_sum;    }     // Driver function    public static void main (String[] args) {             String s = "123";        System.out.println(combinedSum(s));    } } // This code is contributed by Anuj_ 67
# Python 3 program to find the sum of# elements present in all subsequences  # Returns numeric value of a subsequence of# s. The subsequence to be picked is decided# using bit pattern of num (We pick all those# digits for which there is a set bit in num)def findSubSequence(s, num):     # Initialize the result    res = 0      # till n!=0    i = 0    while (num) :                  # if i-th bit is set        # then add this number        if (num & 1):            res += ord(s[i]) - ord('0')        i+=1                  # right shift i        num = num >> 1      return res  # function to find combined sum# of all individual subsequence sumdef combinedSum(s):     # length of string    n = len(s)          # stores the combined    c_sum = 0      # 2^n-1 subsequences    ran = (1 << n) - 1      # loop for all subsequences    for i in range( ran+1):        c_sum += findSubSequence(s, i)      # returns the combined sum    return c_sum  # driver codeif __name__ == "__main__":         s = "123"    print(combinedSum(s))
// C# program to find the sum of elements// present in all subsequencesusing System; class GFG {         // Returns numeric value of a    // subsequence of s. The subsequence    // to be picked is decided using bit    // pattern of num (We pick all those    // digits for which there is a set    // bit in num)    static int findSubSequence(string s,                                 int num)    {        // Initialize the result        int res = 0;             // till n!=0        int i = 0;        while (num > 0) {                         // if i-th bit is set            // then add this number            if ((num & 1) == 1)                res += s[i] - '0';            i++;                         // right shift i            num = num >> 1;        }             return res;    }     // function to find combined sum    // of all individual subsequence    // sum    static int combinedSum(string s)    {                 // length of string        int n = s.Length;                 // stores the combined        int c_sum = 0;             // 2^n-1 subsequences        int range = (1 << n) - 1;             // loop for all subsequences        for (int i = 0; i <= range; i++)            c_sum += findSubSequence(s, i);             // returns the combined sum        return c_sum;    }     // Driver function    public static void Main()    {        string s = "123";        Console.Write(combinedSum(s));    } } // This code is contributed by Sam007
<?php// PHP program to find the sum of// elements present in all subsequences // Returns numeric value of a subsequence of// s. The subsequence to be picked is decided// using bit pattern of num (We pick all those// digits for which there is a set bit in num)function findSubSequence($s, $num){         // Initialize the result    $res = 0;     // till n!=0    $i = 0;    while ($num) {                 // if i-th bit is set        // then add this number        if ($num & 1)            $res += $s[$i] - '0';        $i++;                 // right shintift i        $num = $num >> 1;    }     return $res;} // function to find combined sum// of all individual subsequence sumfunction combinedSum(string $s){         // length of string    $n = strlen($s);         // stores the combined    $c_sum = 0;     // 2^n-1 subsequences    $range = (1 << $n) - 1;     // loop for all subsequences    for ($i = 0; $i <= $range; $i++)        $c_sum += findSubSequence($s, $i);     // returns the combined sum    return $c_sum;}     // Driver Code    $s = "123";    echo combinedSum($s);     // This code is contributed by Anuj_67?>
<script>// Javascript program to find the sum of elements// present in all subsequences         // Returns numeric value of a    // subsequence of s. The subsequence    // to be picked is decided using bit    // pattern of num (We pick all those    // digits for which there is a set    // bit in num)    function findSubSequence(s,num)    {        // Initialize the result        let res = 0;               // till n!=0        let i = 0;        while (num > 0) {                           // if i-th bit is set            // then add this number            if ((num & 1) == 1)                res += s[i].charCodeAt(0) - '0'.charCodeAt(0);            i++;                           // right shift i            num = num >> 1;        }               return res;    }         // function to find combined sum    // of all individual subsequence    // sum    function combinedSum(s)    {        // length of String        let n = s.length;                   // stores the combined        let c_sum = 0;               // 2^n-1 subsequences        let range = (1 << n) - 1;               // loop for all subsequences        for (let i = 0; i <= range; i++)            c_sum += findSubSequence(s, i);               // returns the combined sum        return c_sum;    }         // Driver function    let s = "123";    document.write(combinedSum(s));              // This code is contributed by avanitrachhadiya2155</script>
Output:  
24 
 
Sam007
vt_m
ukasp
avanitrachhadiya2155
subsequence
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
sprintf() in C
Convert character array to string in C++
How to Append a Character to a String in C
Naive algorithm for Pattern Searching
Hill Cipher
Boyer Moore Algorithm for Pattern Searching
Program to count occurrence of a given character in a string