-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package recursion; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class balanced_paranthesis { | ||
|
||
public ArrayList<String> generate_paranthesis(int n){ | ||
ArrayList<String> a=new ArrayList<>(); | ||
int open=n; | ||
int close=n; | ||
String op=""; | ||
solve(open,close,op,a); | ||
return a; | ||
} | ||
public void solve(int open,int close,String op,ArrayList<String> a) { | ||
if(open==0 && close==0) { | ||
a.add(op); | ||
return; | ||
} | ||
if(open!=0) { | ||
String op1=op; | ||
op1+='('; | ||
solve(open-1,close,op1,a); | ||
} | ||
if(close>open) { | ||
String op2=op; | ||
op2+=')'; | ||
solve(open,close-1,op2,a); | ||
} | ||
return; | ||
} | ||
public static void main(String[] args) { | ||
int n=4; | ||
balanced_paranthesis b=new balanced_paranthesis(); | ||
System.out.println(b.generate_paranthesis(n)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package recursion; | ||
|
||
import java.util.Stack; | ||
|
||
public class delete_stack_mid { | ||
|
||
public static void del_mid(Stack<Integer>s,int k) { | ||
if(s.size()==0)return; | ||
if(k==1) { | ||
s.pop(); | ||
return; | ||
} | ||
int val=s.peek(); | ||
s.pop(); | ||
del_mid(s,k-1); | ||
s.push(val); | ||
} | ||
public static void main(String[] args) { | ||
Stack<Integer> s=new Stack<>(); | ||
s.push(3); | ||
s.push(4); | ||
s.push(1); | ||
s.push(6); | ||
s.push(5); | ||
del_mid(s,s.size()/2+1); | ||
while(s.size()>0) { | ||
System.out.print(s.peek()); | ||
s.pop(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package recursion; | ||
class Node{ | ||
int data; | ||
Node left; | ||
Node right; | ||
Node(int data){this.data=data;left=null;right=null;} | ||
} | ||
public class height_of_binary_tree { | ||
Node root; | ||
int find_height(Node root) { | ||
if(root==null)return 0; | ||
return 1+Math.max(find_height(root.left), find_height(root.right)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
height_of_binary_tree t=new height_of_binary_tree(); | ||
t.root=new Node(10); | ||
t.root.left=new Node(20); | ||
t.root.right=new Node(30); | ||
t.root.left.left=new Node(40); | ||
t.root.left.left.left=new Node(50); | ||
System.out.println("Max height of tree is "+t.find_height(t.root)); | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package recursion; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class josephus_problem { | ||
|
||
int ans=-1; | ||
public void solve(ArrayList<Integer>a, int k,int index) { | ||
if(a.size()==1) { | ||
//System.out.print(a.get(0)); | ||
ans=a.get(0); | ||
return;// a.get(0); | ||
} | ||
index=(index+k)%a.size(); | ||
//System.out.println(index+" "+a.get(index)); | ||
a.remove(index); | ||
solve(a,k,index); | ||
return;//ans; | ||
} | ||
public static void main(String[] args) { | ||
josephus_problem m=new josephus_problem(); | ||
int n=40; | ||
int k=7; | ||
ArrayList<Integer> a=new ArrayList<>(); | ||
for(int i=0;i<n;i++)a.add(i+1); | ||
int index=0; | ||
System.out.print("The person who will be alive is person "); | ||
m.solve(a,k-1,index); | ||
System.out.println(m.ans); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package recursion; | ||
|
||
public class kth_grammar_symbol { | ||
|
||
public static Boolean solve(int n,int k) { | ||
if(n==1 && k==1)return false; | ||
int mid=(int)Math.pow(2, n-1)/2; | ||
if(k<=mid)return solve(n-1,k); | ||
else return !solve(n-1,k-mid); | ||
} | ||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
int n=4; | ||
int k=4; | ||
if(solve(n,k))System.out.print(1); | ||
else System.out.print(0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package recursion; | ||
|
||
public class permuation_with_case_change { | ||
|
||
public static void case_change(String ip,String op) { | ||
if(ip.length()==0) { | ||
System.out.println(op); | ||
return; | ||
} | ||
String op1=op; | ||
String op2=op; | ||
op1+=ip.substring(0,1).toUpperCase(); | ||
op2+=ip.charAt(0); | ||
ip=ip.substring(1); | ||
case_change(ip,op1); | ||
case_change(ip,op2); | ||
return; | ||
} | ||
public static void main(String[] args) { | ||
//ip should be smaller case letter | ||
String ip="abcd"; | ||
String op=""; | ||
case_change(ip,op); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package recursion; | ||
|
||
public class permutation_letter_case_change { | ||
|
||
public static void case_change(String ip,String op) { | ||
if(ip.length()==0) { | ||
System.out.println(op); | ||
return; | ||
} | ||
|
||
String nums="0123456789"; | ||
//System.out.print(nums.contains(ip.substring(0,1))); | ||
if(!nums.contains(ip.substring(0,1))) { | ||
String op1=op; | ||
String op2=op; | ||
op1+=ip.substring(0,1).toLowerCase(); | ||
op2+=ip.substring(0,1).toUpperCase(); | ||
ip=ip.substring(1); | ||
case_change(ip,op1); | ||
case_change(ip,op2); | ||
} | ||
else { | ||
String op1=op; | ||
op1+=ip.charAt(0); | ||
ip=ip.substring(1); | ||
case_change(ip,op1); | ||
} | ||
|
||
return; | ||
} | ||
public static void main(String[] args) { | ||
String ip="a1B62c"; | ||
String op=""; | ||
case_change(ip,op); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package recursion; | ||
|
||
public class permutation_with_spaces { | ||
|
||
public static void permutations(String ip,String op) { | ||
op+=ip.charAt(0); | ||
ip=ip.substring(1); | ||
solve(ip,op); | ||
} | ||
public static void solve(String ip,String op) { | ||
if(ip.length()==0) { | ||
System.out.println(op); | ||
return; | ||
} | ||
String op1=op; | ||
String op2=op; | ||
op1+="_"+ip.charAt(0); | ||
op2+=ip.charAt(0); | ||
ip=ip.substring(1); | ||
solve(ip,op1); | ||
solve(ip,op2); | ||
return; | ||
} | ||
public static void main(String[] args) { | ||
String ip="ABCD"; | ||
String op=""; | ||
System.out.println("The permutations are:"); | ||
permutations(ip,op); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package recursion; | ||
//does not remove duplicates | ||
public class print_subset { | ||
|
||
public static void find_subsets(String ip,String op) { | ||
if(ip.length()==0) { | ||
System.out.println(op); | ||
return; | ||
} | ||
String op1=op; | ||
String op2=op; | ||
op2+=ip.charAt(0); | ||
ip=ip.substring(1); | ||
find_subsets(ip,op1); | ||
find_subsets(ip,op2); | ||
return; | ||
} | ||
public static void main(String[] args) { | ||
String ip="aac"; | ||
String op=""; | ||
find_subsets(ip,op); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package recursion; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class sort_array { | ||
|
||
public static void sort(ArrayList<Integer>a) { | ||
if(a.size()==0)return; | ||
int temp=a.get(a.size()-1); | ||
a.remove(a.size()-1); | ||
sort(a); | ||
insert(a,temp); | ||
} | ||
public static void insert(ArrayList<Integer>a,int temp) { | ||
if(a.size()==0 || a.get(a.size()-1)<=temp) { | ||
a.add(temp); | ||
return; | ||
} | ||
int val=a.get(a.size()-1); | ||
a.remove(a.size()-1); | ||
insert(a,temp); | ||
a.add(val); | ||
|
||
} | ||
public static void main(String[] args) { | ||
ArrayList<Integer>a=new ArrayList<>(); | ||
a.add(1); | ||
a.add(3); | ||
a.add(2); | ||
a.add(8); | ||
a.add(5); | ||
sort(a); | ||
for(int i=0;i<a.size();i++)System.out.print(a.get(i)+" "); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package recursion; | ||
//prints dictinct permutations | ||
//considering boolean array->for distinct(initially dont use) | ||
public class string_permutation { | ||
|
||
public static void print_permu(String ip,String op) { | ||
if(ip.length()==0) { | ||
System.out.print(op+" "); | ||
return; | ||
} | ||
boolean alpha[]=new boolean[26]; | ||
for(int i=0;i<ip.length();i++) { | ||
char ch=ip.charAt(i); | ||
String res=ip.substring(0,i)+ip.substring(i+1); | ||
System.out.println(res); | ||
System.out.println(op); | ||
if(alpha[ch-'a']==false) | ||
print_permu(res,op+ch); | ||
alpha[ch-'a']=true; | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String ip="abc"; | ||
print_permu(ip,""); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package recursion; | ||
|
||
public class tower_of_hanoi { | ||
int count=0; | ||
public void TOH(int s,int d,int h,int n) { | ||
if(n==1) { | ||
System.out.println("Moving plate "+n+" from "+s+"->"+d); | ||
count++; | ||
return; | ||
} | ||
TOH(s,h,d,n-1); | ||
System.out.println("Moving plate "+n+" from "+s+"->"+d); | ||
count++; | ||
TOH(h,d,s,n-1); | ||
} | ||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
tower_of_hanoi han=new tower_of_hanoi(); | ||
int n=3; | ||
int s=1,h=2,d=3; | ||
han.TOH(s,d,h,n); | ||
System.out.println(han.count); | ||
} | ||
|
||
} |
Oops, something went wrong.