Given 2 numbers a and b of same length. The task is to calculate their sum in such a way that when adding two corresponding positions the carry has to be kept with them only instead of propagating to the left.
See the below image for reference:
Input: a = 7752 , b = 8834 Output: 151586 Input: a = 123 , b = 456 Output: 579
Approach: First of all, reverse both of the numbers a and b. Now, to generate the resulting sum:
- Extract digits from both a and b.
- Calculate sum of digits.
- If sum of digits is a single digit number, append it directly to the resultant sum.
- Otherwise, reverse the current calculated digit sum and extract digits from it one by one and append to the resultant sum.
Below is the implementation of the above approach:
Time Complexity: O(N).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the number of carry operations required to add two numbers
- Count of carry operations on adding two Binary numbers
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Split N into two integers whose addition to A and B makes them equal
- BCD addition of given Decimal numbers
- Check perfect square using addition/subtraction
- Number Theory | Generators of finite cyclic group under addition
- Addition and Subtraction of Matrix using pthreads
- Add N digits to A such that it is divisible by B after each addition
- Modify array by merging elements with addition such that it consists of only Primes.
- Number formed after K times repeated addition of smallest divisor of N
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Minimum cost to convert M to N by repeated addition of its even divisors
- Minimum repeated addition of even divisors of N required to convert N to M
- Add two numbers without using arithmetic operators
- To find sum of two numbers without using any operator
- Finding LCM of more than two (or array) numbers without using GCD
- Compute average of two numbers without overflow
- Largest of two distinct numbers without using any conditional statements or operators
- Find HCF of two numbers without using recursion or Euclidean algorithm
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.