Advertisement
nikunjsoni

1354

May 9th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool isPossible(vector<int>& target) {
  4.         // Special case of 1 number in array.
  5.         if(target.size() == 1)
  6.             if(target[0] == 1)
  7.                 return true;
  8.             else
  9.                 return false;
  10.        
  11.         long long int sum=0;
  12.         priority_queue<long long int> pq;
  13.        
  14.         for(auto &num: target){
  15.             sum += num;
  16.             pq.push(num);
  17.         }
  18.  
  19.         while(pq.size() > 1){
  20.             if(sum == pq.size()) return true;
  21.             long long int largest = pq.top();
  22.             pq.pop();
  23.             long long int rest = sum-largest;
  24.             if(rest == 1) return true;
  25.             long long int newNum = largest % rest;
  26.             if(newNum < 1 || newNum == largest) return false;
  27.             pq.push(newNum);
  28.             sum = sum-largest+newNum;
  29.         }
  30.         return true;
  31.     }
  32. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement