Given two numbers N1 and N2 represented by two stacks, such that their most significant digits are present at the bottom of the stack, the task is to calculate and return the sum of the two numbers in the form of a stack.
**Examples: **
Input:_ N1={5, 8, 7, 4}, N2={2, 1, 3} _
Output:_ {6, 0, 8, 7}_
Explanation:
Step 1: Popped element from N1(= 4) + Popped element from N2(= 3) = {7} and rem=0.
Step 2: Popped element from N1(= 7) + Popped element from N2(= 1) = {7, 8} and rem=0.
Step 3: Popped element from N1(= 8) + Popped element from N2(= 2) = {7, 8, 0} and rem=1.
Step 4: Popped element from N1(= 5) = {7, 8, 0, 6}
On reverse the stack, the desired arrangement {6,0,8,7} is obtained.
Input_: N1={6,4,9,5,7}, N2={213} _
Output:{6, 5, 0, 0, 5}
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: The problem can be solved using the concept of Add two numbers represented by linked lists. Follow the below steps to solve the problem.
#data structures #mathematical #stack #big data