-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowersTwo.java
More file actions
62 lines (60 loc) · 1.39 KB
/
PowersTwo.java
File metadata and controls
62 lines (60 loc) · 1.39 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
import java.io.*;
import java.util.*;
public class PowersTwo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int tempSum =0,last=0;
int sum = sc.nextInt();
int powlen = sc.nextInt();
int[] arr = new int[powlen];
for(int i=0,k=0;i<powlen; i++){
last=i;
if(powlen == 1 && isPowerOfTwo(sum)){
System.out.println("YES"+"\n"+sum);
return;
}else if(tempSum < sum){
int rem = sum - tempSum;
if(rem >= (int)Math.pow(2,k)){
arr[i] = (int)Math.pow(2,k);
tempSum+= arr[i];
k++;
}else{
//decrement k until reminder equals some power of 2
while(rem < (int)Math.pow(2,k)){
k--;
}
arr[i] = (int)Math.pow(2,k);
tempSum+=arr[i];
}
}
if(arr[i]==0){
break;
}
System.out.println("arr=="+arr[i]);
}
if(tempSum == sum && last==powlen-1){
PowersTwo.printArrayElements(arr);
}else{
System.out.println("NO");
}
}
public static boolean isPowerOfTwo(int num){
return (num & (num-1))==0;
}
public static void printArrayElements(int[] array){
//sort the elements in the array
for(int i=0; i< array.length-1;i++){
for(int j=i+1; j< array.length; j++){
if(array[i] > array[j]){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("YES");
for(int i=0; i<array.length; i++){
System.out.print(array[i]+" ");
}
}
}