Delete middle element of a stack | Practice | GeeksforGeeks

Given a stack with push(), pop(), empty() operations, delete the middle of the stack without using any additional data structure.
Middle: ceil((size_of_stack+1)/2) (1-based index)
 
Example 1:
Input: 
Stack = {1, 2, 3, 4, 5}
Output:
ModifiedStack = {1, 2, 4, 5}
Explanation:
As the number of elements is 5 , 
hence the middle element will be the 3rd
element which is deleted

Example 2:
Input: 
Stack = {1 2 3 4}
Output:
ModifiedStack = {1 3 4}
Explanation:
As the number of elements is 4 , 
hence the middle element will be the 2nd
element which is deleted
 
Your Task:
You don't need to read input or print anything. Complete the function deleteMid() which takes the stack and its size as input parameters and modifies the stack in-place.
Note: The output shows the stack from top to bottom. 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
2 ≤ size of stack ≤ 100
0
vishalsavade1 hour ago
//This code is contributed by Vishal Savade
// Easy way to understand recursion
void foo(stack<int> &s, int cnt){
        if(cnt == 0){ s.pop(); return;}
        int top = s.top();
        s.pop();
        foo(s, cnt-1);
        s.push(top);
    }
    void deleteMid(stack<int>&s, int sizeOfStack)
    {
        // code here..
        int mid = 0;
        mid = (sizeOfStack/2);
        foo(s, mid);
    }
+1
shubhamkhavare2 days ago
Easy Java Code: 
       int mid = sizeOfStack/2;       if(sizeOfStack%2 == 0)       {           mid = mid - 1;       }       s.removeElementAt(mid);
+1
adarshgupta4011 week ago
Easy Recursive Java Code=
class Solution
{
    public void deleteMid(Stack<Integer>s,int sizeOfStack){
        int middle = sizeOfStack/2;
        if(s.isEmpty()) return ;
        deleteMiddle( s, middle);
     } 
    
    public void deleteMiddle(Stack<Integer> s, int middle ){
        if( middle == 0 ) {
            s.pop();
            return ;
        }
        
        int temp = s.pop();
        deleteMiddle(s, middle-1 );
        s.push(temp);
        return ;
    }
}
0
madhajaswanth1 week ago
 public void deleteMid(Stack<Integer>s,int n){              Stack<Integer> stack = new Stack<>();          // removes middle -1 elements       for(int i=0;i<m;i++){           stack.push(s.pop());           }       // removes middle element       s.pop();              // adds the first removed elements       while(!stack.isEmpty()){           s.push(stack.pop());       }   } 
0
ns2847192 weeks ago
void solve(stack<int>&s, int count , int size)    {        if(count == size/2)        {            s.pop();            return;        }                int num = s.top();        s.pop();        solve(s,count+1,size);        s.push(num);    }   void deleteMid(stack<int>&s, int sizeOfStack)   {       // code here..        int count=0;       solve(s,count,sizeOfStack);          }
-1
kunal chalotra2 weeks ago
JAVA SOLUTION  
Stack<Integer> st= new Stack();       int middle=Math.abs(sizeOfStack/2);       int count=0;       while(!s.empty())       {           st.push(s.pop());           count++;           if(count==middle)           {               s.pop();               break;           }       }       while(!s.empty())       {           st.push(s.pop());       }       while(!st.empty())       {           s.push(st.pop());       }
0
dipanshusharma93134 weeks ago
// java solution
class Solution{   public void deleteMid(Stack<Integer>s,int sizeOfStack){       if(sizeOfStack%2==0){       if(sizeOfStack/2 == s.size()){           s.pop();           return;       }       }       if(sizeOfStack%2!=0){           if((sizeOfStack/2)+1 == s.size()){           s.pop();           return;       }       }       int temp = s.pop();       deleteMid(s, sizeOfStack);       s.push(temp);   } }
0
tarunkanade4 weeks ago
0.5/1.6
public void deleteMid(Stack<Integer>s,int sizeOfStack){
        // code here
        int toBeDeleted = (int)Math.ceil((sizeOfStack+1)/2.0);
        
        ArrayDeque<Integer> t = new ArrayDeque<>();
        
        int count = 1;
        
        while(count <= toBeDeleted){
            if(count == toBeDeleted){
                s.pop();
                break;
            }else{
                t.push(s.pop());
            }
            count++;
        }
        
        while(!t.isEmpty()){
            s.push(t.pop());
        }
    } 
-1
sadiiqyare661 month ago
public void deleteMid(Stack<Integer>s,int sizeOfStack){
        // code here
        
        int n = (sizeOfStack/2)+1;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        
        
        for (int i = 0; i < n-1; i++ ){
            int h = s.pop();
            arr.add(h);
        }
        s.pop();
        for (int i = arr.size()-1; i >= 0; i--){
            s.push(arr.get(i));
        }
    } 
0
anujrai0408200201 month ago
class Solution
{
    //Function to delete middle element of a stack.
    public void deleteMid(Stack<Integer>s,int sizeOfStack){
        s.removeElementAt((sizeOfStack-1)/2);
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.