1354. Construct Target Array With Multiple Sums
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public boolean isPossible(int[] target) {
if(target.length == 1) {
return target[0] == 1;
}
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b -a);
int sum = 0;
for(int n: target) {
sum += n;
pq.add(n);
}
while(pq.peek() != 1) {
int num = pq.poll();
sum -= num;
if(sum < 1 || sum >= num) {
return false;
}
num %= sum;
sum += num;
pq.add(num);
}
return true;
}
}