-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowersOptimal.java
95 lines (93 loc) · 2.11 KB
/
PowersOptimal.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.*;
import java.util.*;
public class PowersOptimal{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int k=0,i=0;
int sum = sc.nextInt();
int powlen = sc.nextInt();
int[] result = new int[powlen];
if(powlen ==1){
if((sum&(sum-1))==0){
System.out.println("YES");
System.out.println(sum);
}else{
System.out.println("NO");
}
}else if (powlen == sum || sum%powlen==0){
System.out.println("YES");
for(int p=0;p<powlen;p++){
System.out.print("1 ");
}
if(sum > powlen){
int rem = sum -powlen;
for(int p=0;p<rem/2;p++){
System.out.print("2 ");
}
}
} else{
String bits = Integer.toBinaryString(sum);
char[] bitArray = bits.toCharArray();
for(i=bitArray.length-1; i>=0; i--){
if(k==powlen){
break;
}
if(bitArray[i]=='1'){
result[k++] = 1 << (bitArray.length-i-1);
}
}
if(powlen == k && i==-1 ){
// print the array
System.out.println("YES");
for(int j=0;j<powlen;j++){
System.out.print(result[j]+" ");
}
}else if(k<powlen){
//
int tempSum =0;
for(int m=0,l=0;m<powlen; m++){
if(tempSum < sum){
k=m;
int rem = sum - tempSum;
if(rem >= (int)Math.pow(2,l)){
result[m] = (int)Math.pow(2,l);
tempSum+= result[m];
l++;
}else{
//decrement l until reminder equals some power of 2
while(rem < (int)Math.pow(2,l)){
l--;
}
result[m] = (int)Math.pow(2,l);
tempSum+=result[m];
}
}
}
if(tempSum == sum && k==powlen-1){
PowersOptimal.printArrayElements(result);
}else{
System.out.println("NO");
}
}
else{
System.out.println("NO");
}
}
}
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]+" ");
}
}
}