You are given an array target of n integers. From a starting array arr consisting of n 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 &lt;= i &lt; n and set the value of arr 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 arr, otherwise, return false.

&nbsp;
Example 1:

Input: target = [9,3,5]
Output: true
Explanation: Start with arr = [1, 1, 1] 
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done


Example 2:

Input: target = [1,1,1,2]
Output: false
Explanation: Impossible to create target array from [1,1,1,1].


Example 3:

Input: target = [8,5]
Output: true


&nbsp;
Constraints:


	n == target.length
	1 &lt;= n &lt;= 5 * 104
	1 &lt;= target[i] &lt;= 109


class Solution:
    def isPossible(self, target: List[int]) -> bool:
        if len(target) == 1:
            return target == [1]
        res = sum(target)
        heap = [-elem for elem in target]
        heapify(heap)
        while heap[0]<-1:
            maximum = -heappop(heap)
            res -= maximum

            if res == 1:
                return True
            x = maximum % res
            if x == 0 or (x != 1 and x == maximum):
                return False

            res += x
            heappush(heap,-x)
        return True
class Solution {
    public boolean isPossible(int[] target) {
        if(target.length==1) return target[0]==1;

        PriorityQueue<Integer> que = new PriorityQueue<Integer>(Collections.reverseOrder());
        int totsum = 0;

        for(int i=0;i<target.length;i++){
            que.add(target[i]);
            totsum += target[i];
        }

        while(que.peek()!=1){
            int max = que.remove();
            int rem = totsum-max;
            int maxprev = max % rem;
            totsum = rem+maxprev;

            if(rem==1) return true;

            if(maxprev == 0 || maxprev == max){
                return false;
            } else {
                que.add(maxprev);
            }
        }

        return true;
    }
}
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;
    }
};
var isPossible = function(target) {
    let max=0
    let index=-1

    for(let i=target.length-1;i>=0;i--){
        if(target[i]>max){
            max=target[i]
            index=i
        }
    }
    if(max===1)return true // if max itself is 1 return true

    let total=0
    for(let i=0;i<target.length;i++){
        if(i!==index){
            total+=target[i]
        }
    }
    // If total=1,it means only one element was remaining apart from max and its value is 1 return true
    // eg target=[10,1] we started with [1,1] so next steps would be [2,1]->[3,1]->...[10,1] we can make sure it leads to target
    if(total===1)return true;
     // max should be greater than remaining nums sum OR if total is 0 it would lead to infinite loop( num%0 === NaN) so return false
    if(max<=total||total===0)return false;
    max=max%total;
    if(max<1)return false; // it should not be less than 1
    target[index]=max;

    return isPossible(target)
};
Construct Target Array With Multiple Sums