https://blog.csdn.net/lichong_87/article/details/6860329
https://www.geeksforgeeks.org/difference-of-two-large-numbers/
https://www.geeksforgeeks.org/difference-of-two-large-numbers/
Given two numbers as strings. The numbers may be very large (may not fit in long long int), the task is to find difference of these two numbers.
We can avoid the first two string reverse operations by traversing them from end. Below is optimized solution.
This is simple based on school mathematics. We traverse both strings from end, one by one subtract digits.
1) Reverse both strings.
2) Keep subtracting digits one by one from 0’th index (in reversed strings) to end of smaller string, append the diff if it’s positive to end of result. If difference(diff) is negative then add 10 and keep track of carry as 1 if it’s positive then carry is 0.
3) Finally reverse the result.
1) Reverse both strings.
2) Keep subtracting digits one by one from 0’th index (in reversed strings) to end of smaller string, append the diff if it’s positive to end of result. If difference(diff) is negative then add 10 and keep track of carry as 1 if it’s positive then carry is 0.
3) Finally reverse the result.