MAY DAILY CODING CHALLENGE
1 MAY
2 MAY
3 MAY
4 MAY
5 MAY
6 MAY
7 MAY
8 MAY
9 MAY
CONSTRUCT TARGET ARRAY WITH MULTIPLE SUMS
Given an array of integers target. From a starting array, A consisting of all 1's, you may perform the following procedure :
- let
xbe the sum of all elements currently in your array. - choose index
i, such that0 <= i < target.sizeand set the value ofAat indexitox. - You may repeat this procedure as many times as needed.
Return True if it is possible to construct the target array from A otherwise return False.
bool isPossible(vector<int>& A) {
long total = 0;
int n = A.size(), x;
priority_queue<int>pq(A.begin(), A.end());
for(int a : A)
total += a;
while(true){
x = pq.top();
pq.pop();
total -= x;
if(x == 1 || total == 1)
return true;
if(x < total || total == 0 || x % total == 0)
return false;
x = x % total;
total += x;
pq.push(x);
}
}
10 MAY
No comments:
Post a Comment