Chocolate stack
Practice
1.5 (10 votes)
Basics of stacks
Data structures
Stacks
Real world
Problem
58% Success 5448 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A shop has a stack of chocolate boxes each containing a positive number of chocolates. Initially, the stack is empty. During the next N minutes, either of these two things may happen:

  • The box of chocolates on top of the stack gets sold
  • You receive a box of chocolates from the warehouse and put it on top of the stack.

Determine the number of chocolates in the sold box each time he sells a box.

Notes

  • If C[i] = 0, he sells a box. If C[i] > 0, he receives a box containing C[i] chocolates.
  • It is confirmed that he gets a buyer only when he has a non-empty stack.
  • The capacity of the stack is infinite.

Example 1

Assumptions

Input

  • N = 4
  • C = [2, 1, 0, 0]

Output: 1 2

Approach

After the first two minutes, the stack is [1, 2].

During the third minute, the box on the top having 1 chocolate is sold.

During the fourth minute, the box on the top having 2 chocolates is sold.

Function description

Complete the function solve() provided in the editor. The function takes the following 2 parameters and returns the solution.

  • N: Represents the number of minutes
  • C: Represents the description of boxes

Input format for custom testing

Note: Use this input format if you are testing against custom input or writing code in a language where we don’t provide boilerplate code

  • The first line contains N denoting the number of minutes.
  • The second line contains C denoting the array consisting of the box descriptions.

Output format

Print an array, representing the number of chocolates in the sold box each time you sell a box.

Constraints

\(1 \le N \le 10^5 \\ 0 \le C[i] \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
44 votes
Tags:
Data StructuresEasyStacks
Points:20
10 votes
Tags:
StacksBasics of StacksSetsData Structures
Points:20
48 votes
Tags:
Data StructuresEasyHiringQueueStacks