1441. Build an Array With Stack Operations LeetCode Solution
In this guide, you will get 1441. Build an Array With Stack Operations LeetCode Solution with the best time and space complexity. The solution to Build an Array With Stack Operations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Build an Array With Stack Operations solution in C++
- Build an Array With Stack Operations solution in Java
- Build an Array With Stack Operations solution in Python
- Additional Resources

Problem Statement of Build an Array With Stack Operations
You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
“Push”: pushes an integer to the top of the stack.
“Pop”: removes the integer on the top of the stack.
You also have a stream of the integers in the range [1, n].
Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
If the stack is not empty, pop the integer at the top of the stack.
If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.
Example 1:
Input: target = [1,3], n = 3
Output: [“Push”,”Push”,”Pop”,”Push”]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Pop the integer on the top of the stack. s = [1].
Read 3 from the stream and push it to the stack. s = [1,3].
Example 2:
Input: target = [1,2,3], n = 3
Output: [“Push”,”Push”,”Push”]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Read 3 from the stream and push it to the stack. s = [1,2,3].
Example 3:
Input: target = [1,2], n = 4
Output: [“Push”,”Push”]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
The answers that read integer 3 from the stream are not accepted.
Constraints:
1 <= target.length <= 100
1 <= n <= 100
1 <= target[i] <= n
target is strictly increasing.
Complexity Analysis
- Time Complexity:
- Space Complexity:
1441. Build an Array With Stack Operations LeetCode Solution in C++
class Solution:
def buildArray(self, target: list[int], n: int) -> list[str]:
ans = []
i = 0 # Target pointer
num = 1 # Curr num
while i < len(target):
t = target[i]
if t == num:
ans.append('Push')
i += 1
else:
ans.append('Push')
ans.append('Pop')
num += 1
return ans
/* code provided by PROGIEZ */
1441. Build an Array With Stack Operations LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1441. Build an Array With Stack Operations LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.