Skip to content

Commit 2605f7b

Browse files
authored
Create Delete_Middle_Element_Stack_Recursion.java
1 parent 87ccee8 commit 2605f7b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
//Problem :: Delete a middle ele of Stack :::
3+
//Using Recursion :::
4+
5+
public class Main
6+
{
7+
public static void main(String[] args) {
8+
Stack<Integer> st = new Stack<>();
9+
st.push(9);
10+
st.push(-4);
11+
st.push(2);
12+
st.push(7);
13+
st.push(11);
14+
15+
int mid =(st.size()/2)+1;
16+
deleteMidStack(st,mid);
17+
//TC : O(n)
18+
//SC : O(1)
19+
//Auxillary Space : O(n) -- Recursion Stack
20+
System.out.println(st);
21+
22+
}
23+
public static void deleteMidStack(Stack<Integer> st,int indx){
24+
//B H I
25+
//Base case ::
26+
if(indx==1){
27+
st.pop();
28+
return;
29+
}
30+
//Hypothesis::
31+
int ele =st.pop();
32+
//Induction:::
33+
deleteMidStack(st,indx-1);
34+
st.push(ele);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)