Sum of all subsequences of a number | 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 ) ; Initialize the result ; till n != 0 ; if i - th bit is set then add this number ; right shift i ; function to find combined sum of all individual subsequence sum ; length of String ; stores the combined ; 2 ^ n - 1 subsequences ; loop for all subsequences ; returns the combined sum ; Driver function
function findSubSequence ( s , num ) { let res = 0 ; let i = 0 ; while ( num > 0 ) { if ( ( num & 1 ) == 1 ) res += s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; i ++ ; num = num >> 1 ; } return res ; } function combinedSum ( s ) { let n = s . length ; let c_sum = 0 ; let range = ( 1 << n ) - 1 ; for ( let i = 0 ; i <= range ; i ++ ) c_sum += findSubSequence ( s , i ) ; return c_sum ; } let s = " " ; document . write ( combinedSum ( s ) ) ;