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).
- Count the number of carry operations required to add two numbers
- Add N digits to A such that it is divisible by B after each addition
- Addition and Subtraction of Matrix using pthreads
- Check perfect square using addition/subtraction
- Number Theory | Generators of finite cyclic group under addition
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Numbers within a range that can be expressed as power of two numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.