Delete Middle Element from Stack

You are provided with a stack ARR of size 'N+1'. Your task is to delete the middlemost element so that the size of the resulting stack becomes 'N'.

Recall that a stack is a linear data structure where both insertion and deletion of elements occur at the top, following the LIFO (Last In First Out) or FILO (First In Last Out) principles.

Input:

The first line of input consists of an integer 'T', the number of test cases.
Each test case begins with an integer 'N', indicating that the stack initially contains 'N+1' elements.
The following line in each test case includes 'N+1' space-separated integers representing stack elements.

Output:

For each test case, output 'N' space-separated integers showing the stack after the middle element is removed.
Each test case's result should be printed on a new line.

Example:

Input: ARR = [1, 2, 3, 4, 5], N = 4
Output: ARR = [1, 2, 4, 5]

Input: ARR = [5, 6, 7, 8], N = 3
Output: ARR = [5, 7, 8]

Constraints:

  • 1 <= T <= 100
  • 1 <= N+1 <= 3000
  • 0 <= data <= 10^9
Note:
No need to print anything; focus on implementing the function correctly.
AnswerBot
5d

Implement a function to delete the middle element from a stack, reducing its size by 1.

  • Iterate through the stack to find the middle element based on the stack size.

  • Remove the middle element and shift ...read more

Help your peers!
Add answer anonymously...
MedTourEasy Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter