Sunday, May 9, 2021

LEETCODE DAILY CODING CHALLENGES SOLUTIONS

 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

REFER

Given an array of integers target. From a starting array, A consisting of all 1's, you may perform the following procedure :

  • let x be the sum of all elements currently in your array.
  • choose index i, such that 0 <= i < target.size and set the value of A at index i to x.
  • 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

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...