-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic.java
More file actions
67 lines (44 loc) · 1.48 KB
/
Copy pathBasic.java
File metadata and controls
67 lines (44 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package recursion;
import java.util.HashSet;
import java.util.Set;
public class Basic {
static int sumOfDigits(int n) {
if (n == 0) return 0;
int lastDigit = n % 10;
return lastDigit + sumOfDigits(n / 10);
}
static int printSumNaturalNumber(int n) {
if (n == 0) return 0;
return printSumNaturalNumber(n - 1) + n;
}
static int powerOfNumber(int base, int exponent) {
if (exponent == 0) return 1;
return base * powerOfNumber(base, exponent - 1);
}
static Set<String> generateSubsets(String s) {
Set<String> set = new HashSet<>();
return utilSubsets(s, 0, " ", set);
}
static Set<String> utilSubsets(String s, int i, String curr, Set<String>set) {
if (i == s.length()) {
set.add(curr);
} else {
utilSubsets(s, i + 1, curr, set);
utilSubsets(s, i + 1, curr + s.charAt(i), set);
}
return set;
}
//Time complexity--> 2^n -1
static void towerOfHanoi(int n, char from, char to, char aux) {
if (n == 0) return;
towerOfHanoi(n - 1, from, aux, to);
System.out.println("Move " + n + " from " + from + " to " + to);
towerOfHanoi(n - 1, aux, to, from);
}
public static void main(String[] args) {
// int ans = printSumNaturalNumber(5);
// int ans = powerOfNumber(2,5);
// Set<String> ans = generateSubsets("abc");
towerOfHanoi(3, 'A', 'C', 'B');
}
}