Delete the Middle Element of a Stack Using Recursion

Solve this Problem
Easy20–25 min
Topics
Companies
Practice:GFG ↗
Given a stack of integers, delete the middle element — for an odd-length stack, the exact middle; for an even-length stack, the one closer to the bottom of the two middle elements — using only push, pop, top, and isEmpty, with no other explicit data structure or direct indexing. Since the middle position can't be accessed directly under this constraint, the recursion reaches it a different way: by counting. Popping one element per level of recursive descent means the depth of recursion IS the count of elements removed from the top so far — so descending exactly ⌊n/2⌋ levels guarantees the current top is precisely the target element, without ever computing or storing an index. Pop and discard it there; then, as the recursion unwinds, every other held-aside value gets pushed back on top, restoring the stack everywhere except at the one dropped position.

Test Case 1:

Input:stack = [7, 3, 9, 2, 8]
Output:[7, 3, 2, 8]
Explanation:5 elements — the exact middle (9, at 1-indexed bottom position 3) is removed.

Test Case 2:

Input:stack = [9, 4, 6, 2, 7, 1]
Output:[9, 4, 2, 7, 1]
Explanation:6 elements — position ⌈6/2⌉=3 from the bottom (value 6) is removed.

Test Case 3:

Input:stack = [1, 2, 3, 4]
Output:[1, 3, 4]
Explanation:4 elements — position ⌈4/2⌉=2 from the bottom (value 2) is removed, the lower of the two middle elements.

Constraints

  • 1 ≤ stack.length ≤ 11
  • stack is given bottom-to-top (index 0 is the bottom, the last index is the top)
  • -100 ≤ stack[i] ≤ 100
  • The 'middle' element, counted 1-indexed from the bottom, is at position ⌈n/2⌉ — for an even-length stack, this is the one closer to the bottom of the two middle elements
  • Only push, pop, top/peek, and isEmpty are considered valid stack operations for the intended (recursive) technique — no other explicit data structure or index-based access may be used
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Compute the Index, Rebuild the Array

Good

If direct indexed access is allowed, this is immediate: compute the 0-indexed position to remove (⌈n/2⌉-1, counting from the bottom), then build a new array copying every element except the one at that position. Simple and linear, but it reaches past the "only stack operations" constraint by indexing directly into the middle of the structure — something a real stack ADT (LIFO access only) doesn't actually support.

TimeO(n)
SpaceO(n)
1class Solution { 2 public int[] deleteMiddleRecursive(int[] stack) { 3 int n = stack.length; 4 int removeIdx = (int) Math.ceil(n / 2.0) - 1; 5 int[] result = new int[n - 1]; 6 int idx = 0; 7 for (int i = 0; i < n; i++) { 8 if (i != removeIdx) result[idx++] = stack[i]; 9 } 10 return result; 11 } 12}

Optimal — Pure Recursion, No Auxiliary Data Structure

Optimal

Track how many elements have been popped so far as the recursion descends. Pop the top, hold it in the current call's local variable, and recurse — until the count of pops reaches the target depth (⌊n/2⌋), at which point the element now on top is exactly the middle one: pop it and simply don't restore it. As the recursion unwinds, every other held-aside value gets pushed back, restoring the stack's original order everywhere except at the one position that was dropped. No index is ever computed or used — the recursion counts its way there using only push and pop.

TimeO(n)
SpaceO(n) call-stack space, no explicit auxiliary structure
1class Solution { 2 public int[] deleteMiddleRecursive(int[] stack) { 3 List<Integer> list = new ArrayList<>(); 4 for (int x : stack) list.add(x); 5 deleteHelper(list, 0, list.size()); 6 int[] result = new int[list.size()]; 7 for (int i = 0; i < list.size(); i++) result[i] = list.get(i); 8 return result; 9 } 10 11 private void deleteHelper(List<Integer> s, int curr, int n) { 12 if (curr == n / 2) { 13 s.remove(s.size() - 1); 14 return; 15 } 16 int top = s.remove(s.size() - 1); 17 deleteHelper(s, curr + 1, n); 18 s.add(top); 19 } 20}

Related Problems