forked from rohan472000/Java-DSA-InterviewPrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_n_power.java
33 lines (31 loc) · 879 Bytes
/
x_n_power.java
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
public class x_n_power {
public static void main(String[] args) {
int x=2;
int n=5;
System.out.println(calcpower(x, n));
}
public static int calcpower(int x,int n){
if(n==0) return 1;
if(x==0) return 0;
//n is even
if(n % 2 ==0)
return calcpower(x,n/2)*calcpower(x,n/2);
else
return calcpower(x, n/2)*calcpower(x, n/2)*x;
}
}
/*
* public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for(int n : nums){
int size = result.size();
for(int i=0; i<size; i++){
List<Integer> subset = new ArrayList<>(result.get(i));
subset.add(n);
result.add(subset);
}
}
return result;
}
*/