Addition of digits of two number without giving carry to other digit and return type should be number only
In this article, we are going to solve a problem in which we will be given two numbers a and b and we have to add the digits of the given number and pass the carry to the other digit instead of whatever the result is coming after the addition of digits write it and so on.
Example:
Input
a = 48368
b = 3224
Output:
4115812
Explanation:
4 8 3 6 8
+ 0 3 2 2 4
Answer = 4 11 5 8 12
So, as you can see above we have added every digit of the number without passing carry to another digit.
Note:- The return of output after the addition of two numbers should be an integer only.
Efficient Algorithm for the Above Problem
Let us have two number
a = 48368
b = 3224
Step 1: First, we will be splitting both numbers into digits and will store the array as given below: -
arr1 = [ 4, 8, 3, 6, 8 ]
arr2 = [ 3, 2, 2, 4 ]
Step 2: After splitting and storing we will make the length of both arrays equal to each other by adding the 0 in their beginning whose length is less than the other array so that the addition can be performed easily as explained below: -
As we can see the arr2 length is less as compared to arr1. So,
arr2 = [ 0, 3, 2, 2, 4 ]
Step 3: Now, we will add every digit of the array either from the last index or the start index(as per your choice) and will store sum into the another array as explained below: -
We are adding the digits from starting index and will push them into the array.
arr1 = [ 4, 8, 3, 6, 8 ]
arr2 = [ 0, 3, 2, 2, 4 ]
digitSum = [4, 11, 5, 8, 12 ]
Step 4: Finally we have to join the numbers of digitSum array from the start index up to the end index as explained below: -
Ans = 4115812
Step 5: Print the final answer.
Implementation of the Above approach as given below: -
#include<bits/stdc++.h>
using namespace std;
// Logic for storing the digits into the array
void getDigits(vector<int> &v, int a){
while(a != 0){
v.push_back(a%10);
a /= 10;
}
reverse(v.begin(), v.end());
}
// logic for inserting the 0 at the begining
void insertDataAtBegin(vector<int> &v, int size){
for(int i = 0; i < size; i++){
v.insert(v.begin(), 0);
}
}
// logic for the addition of the digits and storing
// into the new array
vector<int> SumDigits(vector<int> vec1, vector<int> vec2){
vector<int> result;
for(int i = 0; i < vec1.size(); i++){
result.push_back(vec1[i]+vec2[i]);
}
return result;
}
// logic for joining for the numbers of the array
void convertIntoNumber(vector<int> result, long long &num){
string ans;
for(int i = 0; i < result.size(); i++){
if(result[i] < 10){
ans.push_back(char(result[i]+48));
}
else{
ans.push_back(char((result[i]/10)+48));
ans.push_back(char((result[i]%10)+48));
}
}
num = stoi(ans);
}
int main() {
int a = 48368;
int b = 3224;
vector<int> storeA;
vector<int> storeB;
// storing the digits of both number into
// the vector
getDigits(storeA, a);
getDigits(storeB, b);
// Making the size of both vector equal
// sothat we can add the digits easily
if(storeA.size() > storeB.size()){
insertDataAtBegin(storeB, storeA.size()-storeB.size());
}
if(storeB.size() > storeA.size()){
insertDataAtBegin(storeA, storeB.size()-storeA.size());
}
vector<int> result = SumDigits(storeA, storeB);
long long finalAns = 0;
convertIntoNumber(result, finalAns);
cout << finalAns;
cout << endl;
return 0;
}
Output
4115812
Time Complexity: O(N), where N = count of digits of the maximum number
Auxiliary Space: O(N)