Delete Mid Element from Stack Problem Statement

You are provided with a stack "ARR" containing "N+1" elements. Your task is to delete the middlemost element so that the resulting stack contains "N" elements.

Example:

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

In the example above, the stack has an odd number of elements. Therefore, the middle element is the ((N+1)/2)th element, which is removed in the output.

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

In this example, the stack has an even number of elements. Among the two middle elements, we remove the one which appears first. Thus, the middle element is the ((N+1)/2 - 1)th element, which is 6 and is removed in the output.

Input:

The first line contains an integer 'T' representing the number of test cases. For each test case, the first line contains an integer 'N', where 'N+1' indicates the number of elements in the stack initially. The second line contains 'N+1' space-separated integers representing the stack elements.

Output:

For each test case, print 'N' space-separated integers representing the stack elements after removing the middle element. Each test case's output should be printed on a separate line.

Constraints:

  • 1 <= T <= 100
  • 1 <= N+1 <= 3000
  • 0 <= data <= 109
  • Time limit: 1 second

Note: You do not need to print anything; the function should only return the required result.

AnswerBot
5d

Delete the middle element from a stack to reduce its size by one.

  • Identify the middle element based on whether the stack size is odd or even

  • Remove the middle element from the stack

  • Adjust the stack size...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Intern 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