Add two numbers represented by Stacks

Add two numbers represented by Stacks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive.

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}_


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.

  1. Create a new stack,** res** to store the sum of the two stacks.
  2. Initialize variables** rem *and **sum *to store the carry generated and the sum of top elements respectively.
  3. Keep popping the top elements of both the stacks and push the sum % 10 *to *res and update rem as sum/10.
  4. Repeat the above step until the stacks are empty. If rem is greater than 0, insert rem into the stack.
  5. Reverse the res stack so that the most significant digit present at the bottom of the res stack.

data structures mathematical stack big data

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Microsoft big data solutions Companies | Best Microsoft big data Developers

An extensively researched list of top microsoft big data analytics and solution with ratings & reviews to help find the best Microsoft big data solutions development companies around the world.

Silly mistakes that can cost ‘Big’ in Big Data Analytics

‘Data is the new science. Big Data holds the key answers’ - Pat Gelsinger The biggest advantage that the enhancement of modern technology has brought

Big Data can be The ‘Big’ boon for The Modern Age Businesses

We need no rocket science in understanding that every business, irrespective of their size in the modern-day business world, needs data insights for its expansion. Big data analytics is essential when it comes to understanding the needs and wants of a significant section of the audience.

Role of Big Data in Healthcare - DZone Big Data

In this article, see the role of big data in healthcare and look at the new healthcare dynamics. Big Data is creating a revolution in healthcare, providing better outcomes while eliminating fraud and abuse, which contributes to a large percentage of healthcare costs.

How you’re losing money by not opting for Big Data Services?

Big Data Analytics is the next big thing in business, and it is a reality that is slowly dawning amongst companies. With this article, we have tried to show you the importance of Big Data in business and urge you to take advantage of this immense...