Java Collections | Set 6 (Stack) | Practice | GeeksforGeeks

Java provides an inbuilt object type called Stack. It is a collection that is based on the last in first out (LIFO) principle. Try this problem using Stack. 
Given n elements of a stack st where the first value is the bottom-most value of the stack and the last one is the element at top of the stack, delete the middle element of the stack without using any additional data structure.
 
Example 1:
Input: n = 5
st = {1, 2, 3, 4, 5}
Output: 5 4 2 1
Explaination: The middle element is 3. If 
it is deleted and then the values are seen 
from top, this will be the order.
 
Example 2:
Input: n = 6
st = {1, 4, 9, 2, 6, 5}
Output: 5 6 2 4 1
Explaination: The middle element is 9 and if 
it is deleted this will be the stack traversal. 
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function deleteMid() which takes n and st as input parameters and returns a stack where the middle element is deleted.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
 
Constraints:
2 ≤ n ≤ 103
1 ≤ st[i] ≤ 104   
+1
mashhadihossain4 weeks ago
SIMPLE JAVA SOLUTION (0.8/2.2 SEC)
class Solution{   static Stack<Integer> deleteMid(int n, Stack<Integer> st)   {       Stack<Integer> stack=new Stack<Integer>();       int x=n/2;       while(x-->0)       {           stack.push(st.pop());       }       st.pop();       while(!stack.isEmpty())       {           st.push(stack.pop());       }       return st;   }}
+2
ayazmrz982 months ago
static Stack<Integer> deleteMid(int n, Stack<Integer> st)
   {
       int mid=n/2;
       if(n%2==0)
       {
           mid=mid-1;
       }
       st.removeElementAt(mid);
       return st;
   }
0
surajank87573 months ago
st.remove(((n+1)/2)-1);       return (st);
0
hitentandon3 months ago
class Solution{
   static Stack<Integer> deleteMid(int n, Stack<Integer> st)
   {
       LinkedList<Integer> ls = new LinkedList<Integer>(st);//Stack -> List
       ls.remove(((n+1)/2)-1);//removes the mid element
       st = new Stack<Integer>();//releases new memory for the stack
       for(Integer i :ls)
           st.push(i);//Creates a new Stack with all the elements that weren't removed
       return st;//returns the stack    
   }
}
+1
Shikha Suman9 months ago
Shikha Suman
class Solution{    static void delete(int n,Stack<integer> st,int curr)    {        if (st.empty() || curr == n) return;        int x = st.pop();        delete(n, st, curr+1);        if(curr != n/2) st.push(x);            }    static Stack<integer> deleteMid(int n, Stack<integer> st)    {        delete(n,st,0);        return st;    }}
0
Aaditya Burujwale1 year ago
Aaditya Burujwale
Java Solution
0
Sagar Sikchi1 year ago
Sagar Sikchi
Java:
class Solution{    static Stack<integer> deleteMid(int n, Stack<integer> st)    {        Stack<integer> temp = new Stack<>();        int mid = (n + 1) / 2;        int i = (n % 2 == 0) ? 0 : 1;        for(; i < mid; i++) {            temp.add(st.peek());            st.pop();        }        st.pop();        while(!temp.isEmpty()) {            st.add(temp.peek());            temp.pop();        }        return st;    }}
0
Hitesh Yadav3 years ago
Hitesh Yadav
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {    public static void main (String[] args) {        //code        int T;
        Scanner sc= new Scanner(System.in);        T=sc.nextInt();
        while(T>0){            int size=sc.nextInt();          Stack<integer> s1=new Stack<integer>();
          Stack<integer> s2=new Stack<integer>();            int ele=0,result=0;           while(size>0){             ele=sc.nextInt();            result=s1.push(ele);
            size--;          }         // System.out.println(s1);          int s2_result=0;          int s2_size=s1.size();          int mid=0;          if(s2_size%2==0){              mid=s2_size/2;          }          else                {                    mid=s2_size/2;                    mid+=1;                }          while(s2_size>0){            // System.out.print(s2_size);              s2_result=s1.pop();              //System.out.println(s2_result);              if(s2_size!=mid){                  s2.push(s2_result);              }
                s2_size--;          }          //System.out.println(s2);          Iterator itr=s2.iterator();          while(itr.hasNext()){              System.out.print(itr.next() +" ");          }          System.out.println();          T--;
        }
    }}
0
Vishal Bhola3 years ago
Vishal Bhola
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {Scanner sc = new Scanner(System.in);int t = sc.nextInt();while(t-- > 0){    double a=sc.nextDouble();
            int j = (int)Math.ceil(a/2);
    Stack<integer>s = new Stack<integer>();
    for(int i=0;i<a;i++) {="" s.add(sc.nextint());="" if(i="=j-1)" s.pop();="" }="" while(s.size()="">0)    {        System.out.print(s.pop()+" ");    }
            System.out.println();}}}
0
durgesh patidar3 years ago
durgesh patidar
/*package whatever //do not write package name here */import java.util.*;import java.lang.*;import java.io.*;class GFG {public static void main (String[] args) {//codeint t;Scanner sc=new Scanner(System.in);t=sc.nextInt();while(t>0){    int n;    n=sc.nextInt();    Stack s=new Stack();    for(int i=0;i<n;i++) {="" int="" x;="" x="sc.nextInt();" s.add(x);="" }="" int="" mid="(int)(s.size()/2);" if(s.size()%2="=0)" mid--;="" s.remove(s.get(mid));="" collections.reverse(s);="" iterator="" it="s.iterator();" while(it.hasnext())="" {="" system.out.print(it.next()+"="" ");="" }="" system.out.println();="" t--;="" }="" }="" }="" <="" code="">
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.