Sum of all prefixes of given numeric string


In this problem, we need to find the sum of all prefixes of the given string.

The best solution approach is that traverse through each prefix of the string and add them to get the answer.

Problem statement - We have given a string named num_Str containing N digits. We need to find the sum of all prefixes of the given string.

Sample examples

Input

num_str = "1123"

Output

1247

Explanation - All prefixes of the given strings are 1, 11, 112, and 1123. The sum of all prefixes is 1247.

Input

num_str = "1000"

Output

1111

Explanation - 1 + 10 + 100 + 1000 = 1111

Input

num_str = "123456789";

Output

137174205

Explanation - 1 + 12 + 123 + 1234 + 12345 + 123456 + 1234567 + 12345678 + 123456789 = 137174205

Approach 1

In this approach, we will take each prefix of the string and store its sum in one variable. Here, we will implement a function to sum the digits of two strings to get a sum of large strings.

We can use basic mathematics to sum large strings.

Algorithm

Step 1 - Initialize the 'allPrefixSum' with '0' to store the sum of all prefixes and the 'prefix' with an empty string to store the current prefix.

Step 2 - Start traversing the string, and use the '+=' operator to append the current character of the string at the end of the 'prefix'.

Step 3 - Execute the twoDigitSum() function to add the 'prefix' value to the 'allPrefixSum' value and store the function's returned value in the 'allPrefixSum' variable.

Step 3.1 - In twoDigitSum() function, num2's length should be greater than num1. If not, swap both numbers.

Step 3.2 - Initialize the sum_Str string variable to store the sum of num1 and num2.

Step 3.3 - Reverse the num1 and num2 strings to start the sum from the 0th index. In basic math, we start adding digits from the end.

Step 3.4 - Initialize the 'car' variable with 0 to store the carry.

Step 3.5 - Start traversing the num1 string. Take the character from the pth index of num1 and num2, and add them. Also, add carry to value and store the final value in the sum.

Step 3.6 - Perform the sum % 10 operations, and append it to the sum_Str string. In the 'car' store, the sum / 10.

Step 3.7 - Now, covert the remaining num2 string. Add character at pth index to the 'car', take its modulo with 10, and append to sum_str string. Also, update the 'car' value by sum/10.

Step 3.8 - If the value of 'car' is not 0, append it to the sum_Str.

Step 3.9 - Reverse the sum_str value and return it from the function.

Step 4 - At last, return the 'allPrefixSum' variable's value.

Example

#include <bits/stdc++.h>
using namespace std;

string twoDigitSum(string num1, string num2) {
   // We need num2's length larger
   if (num1.length() > num2.length())
      swap(num1, num2);
   // Stores resulting sum
   string sum_str = "";
   // Get size of strings
   int len1 = num1.length(), len2 = num2.length();
   // Reverse strings
   reverse(num1.begin(), num1.end());
   reverse(num2.begin(), num2.end());
   // variable to store car bit
   int car = 0;
   // Traverse num1
   for (int p = 0; p < len1; p++) {
      // Get the sum of digits at the pth index by adding the carry
      int sum = ((num1[p] - '0') + (num2[p] - '0') + car);
      // Update sum_str
      sum_str.push_back(sum % 10 + '0');
      // Update carry to use in the next step
      car = sum / 10;
   }
   // Need to add extra digits of len2
   for (int p = len1; p < len2; p++) {
      // Get the sum of digits, update sum_str, and carry
      int sum = ((num2[p] - '0') + car);
      sum_str.push_back(sum % 10 + '0');
      car = sum / 10;
   }
   // If carry is not 0, add it to sum_str
   if (car)
      sum_str.push_back(car + '0');
   // Reverse resultant string to get answer
   reverse(sum_str.begin(), sum_str.end());
   return sum_str;
}
string getPrefixSum(string num_str) {
   // To store the sum of prefixes
   string allPrefixSum = "0";
   // To store prefix
   string prefix = "";
   // Traverse the string
   for (int p = 0; p < num_str.length(); p++) {
      // Get prefix till pth index
      prefix += num_str[p];
      // Get prefix sum
      allPrefixSum = twoDigitSum(prefix, allPrefixSum);
   }
   // Return Answer
   return allPrefixSum;
}
int main() {
   string num_str = "1123";
   cout << "The sum of all prefixes of the given string is " << 
getPrefixSum(num_str);

   return 0;
}

Output

The sum of all prefixes of the given string is 1247

Time complexity - O(N*N) to add each prefix of the string.

Space complexity - O(N) to store the string prefix.

We can get each prefix of the string while traversing the string. The main logical part of the solution is the summing function to get the sum of the digits of two strings. Programmers can try to get the sum of all suffixes of the given string.

Updated on: 24-Aug-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements