Skip to main content

Stack Designer

Problem Description​

You are given an array arr of size N. You need to push the elements of the array into a stack and then print them while popping.

Examples​

Example 1:

Input: 
n = 5
arr = {1 2 3 4 5}

Output:
5 4 3 2 1

Example 2:

Input: 
n = 7
arr = {1 6 43 1 2 0 5}

Output:
5 0 2 1 43 6 1

Constraints​

  • 1 ≤ Ai ≤ 10^7

Solution for Stack Designer​

Code in Different Languages​

Written by @vansh-codes
 class Stack:
def __init__(self):
self.stack = []

def push(self, arr):
for item in arr:
self.stack.append(item)
return self.stack

def pop_all(self):
while self.stack:
print(self.stack.pop(), end=" ")

# Example usage
arr = [1, 2, 3, 4, 5]
n = len(arr)
s = Stack()
s.push(arr)
s.pop_all()
# Output: 5 4 3 2 1

References​