Given a numeric string str of length N, the task is to find the sum of all possible expressions by inserting the ‘+’ operator between the characters of the string any number of times.
Examples:
Input: str = “125” Output: 176 Explanation: Inserting “+” after 1st index modifies str to “1+25” and value = 26 Inserting “+” after 2nd index modifies str to “12+5” and value = 17 Inserting “+” after both 1st and 2nd index modifies str to “1+2+5” and value = 8 Therefore, the total sum of all possible expression is 125 + 26 + 17 + 8 = 176
Input: str = “9999999999”
Output: 12656242944
Approach: The idea is to insert the ‘+’ operator at all possible index of the string in all possible ways and calculate the sum. Finally, print the total sum obtained. Follow the steps below to solve the problem:
- Initialize a variable say, sumOfExp to store the sum of all possible expression by inserting the ‘+’ operator at all possible indices of the string.
- Generate all possible subset of indices of the string iteratively. For every subset of indices inserts the ‘+’ operator at elements of the subset and increment sumOfExp by the sum of the current expression.
- Finally, print the value of sumOfExp.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSumOfExpressions(string S, int N)
{
unsigned long long sumOfExp = 0;
for ( int i = 0; i < pow (2, N - 1); i++) {
unsigned long long ans_sub = 0;
string subst = string(1, S.at(0));
for ( int j = 0; j < N - 1; j++) {
if (((i >> j) & 1) == 1) {
ans_sub += stoull(subst);
subst = string(1, S.at(j + 1));
}
else
subst += S.at(j + 1);
if (j == N - 2)
ans_sub += stoull(subst);
}
sumOfExp += ans_sub;
}
if (N == 1)
cout << S;
else
cout << sumOfExp;
}
int main()
{
string S = "9999999999" ;
int N = S.length();
findSumOfExpressions(S, N);
}
|
Java
import java.util.List;
import java.util.ArrayList;
class Main
{
static void findSumOfExpressions(String S, int N)
{
long sumOfExp = 0 ;
for ( int i = 0 ; i < Math.pow( 2 , N - 1 ); i++) {
long ans_sub = 0 ;
String subst = "" + S.charAt( 0 );
for ( int j = 0 ; j < N - 1 ; j++) {
if (((i >> j) & 1 ) == 1 ) {
ans_sub += Long.parseLong(subst);
subst = "" + S.charAt(j + 1 );
}
else
subst += S.charAt(j + 1 );
if (j == N - 2 )
ans_sub += Long.parseLong(subst);
}
sumOfExp += ans_sub;
}
if (N == 1 )
System.out.println(S);
else
System.out.println(sumOfExp);
}
public static void main(String[] args)
{
String S = "9999999999" ;
int N = S.length();
findSumOfExpressions(S, N);
}
}
|
Python3
def findSumOfExpressions(S, N):
sumOfExp = 0
for i in range ( 2 * * (N - 1 )):
ans_sub = 0
subst = S[ 0 ]
for j in range (N - 1 ):
if (i >> j) & 1 :
ans_sub + = int (subst)
subst = S[j + 1 ]
else :
subst + = S[j + 1 ]
if j = = N - 2 :
ans_sub + = int (subst)
sumOfExp + = ans_sub
if N = = 1 :
print ( int (S))
else :
print (sumOfExp)
if __name__ = = '__main__' :
S = "9999999999"
N = len (S)
findSumOfExpressions(S, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findSumOfExpressions( string S, int N)
{
ulong sumOfExp = 0;
for ( int i = 0; i < Math.Pow(2, N - 1); i++) {
ulong ans_sub = 0;
string subst = "" + S[0];
for ( int j = 0; j < N - 1; j++) {
if (((i >> j) & 1) == 1) {
ans_sub += Convert.ToUInt64(subst);
subst = "" + S[j + 1];
}
else
subst += S[j + 1];
if (j == N - 2)
ans_sub += Convert.ToUInt64(subst);
}
sumOfExp += ans_sub;
}
if (N == 1)
Console.WriteLine(S);
else
Console.WriteLine(sumOfExp);
}
public static void Main( string [] args)
{
string S = "9999999999" ;
int N = S.Length;
findSumOfExpressions(S, N);
}
}
|
Javascript
function findSumOfExpressions(S, N)
{
let sumOfExp = 0
for ( var i = 0; i < 2 ** (N - 1); i++)
{
let ans_sub = 0
let subst = S[0]
for ( var j = 0; j < N - 1; j++)
{
if (((i >> j) & 1) == 1)
{
ans_sub += parseInt(subst)
subst = S[j + 1]
}
else
subst += S[j + 1]
if (j == N - 2)
ans_sub += parseInt(subst)
}
sumOfExp += ans_sub
}
if (N == 1)
console.log(parseInt(S))
else
console.log(sumOfExp)
}
let S = "9999999999"
let N = S.length
findSumOfExpressions(S, N)
|
Time Complexity: O(2N * N)
Auxiliary Space: O(1)