We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 87ccee8 commit 2605f7bCopy full SHA for 2605f7b
Recursion/Delete_Middle_Element_Stack_Recursion.java
@@ -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