forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct Target Array With Multiple Sums.cpp
37 lines (27 loc) · 1.48 KB
/
Construct Target Array With Multiple Sums.cpp
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
class Solution {
public:
bool isPossible(vector<int>& target) {
//Priority queue for storing all the nums in taget in decreasing order.
priority_queue<int> pq;
long long sum = 0; //for storing total sum
for(auto num : target){ //adding the nums in pq and sum
pq.push(num);
sum+=num;
}
//iterating untill all elements in pq become 1 (in turn pq.top() will also become 1);
while(pq.top() != 1){
sum -= pq.top(); //removing the greatest element as it was last upadted when converting [1,1,1...] array to target. So we are left with sum of other elements.
//when there are elements greeter than 1 then sum of other elements can not be 0 or sum can not be greater than top element because sum + x(any number>0) is pq.top().
if(sum == 0 || sum >= pq.top()) return false;
//if we delete all copies of sum from pq.top() we get an old element.
int old = pq.top() % sum;
//all old elements were > 0 so it can not be 0 unless sum is 1 (This is only possible if array has only 2 elements)
if(sum != 1 && old == 0) return false;
pq.pop(); //Deleting greatest element
pq.push(old); //Adding old element to restore array.
sum += old; //Updating sum
}
//if all elements are 1 then return true
return true;
}
};