id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> ans;\n vector<int> empty;\n // hshTable[value] = frequency\n int hashTable[200001] = {0};\n for(int i = 0; i < changed.size(); i++)\n hashTable[changed[i]]++;\n bool isValid = true;\n if(hashTable[0]%2 != 0)\n isValid = false;\n for(int i = 0; i < hashTable[0]/2; i++)\n ans.push_back(0);\n for(int i = 1; i <= 100000; i++) {\n if(hashTable[i] == 0)\n continue;\n if(hashTable[i*2] < hashTable[i]) {\n isValid = false;\n break;\n }\n else {\n hashTable[i*2] -= hashTable[i];\n for(int j = 0; j < hashTable[i]; j++)\n ans.push_back(i);\n }\n }\n if(!isValid)\n return(empty);\n else\n return(ans);\n }\n};\n",
"memory": "120501"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) { \n int n = changed.size() ; \n vector<int> ans , empty; \n\n if(n % 2 == 1) return empty ; \n\n int num[100005] ; \n memset(num, 0 , sizeof(num)) ;\n for(int x: changed) {\n num[x]++; \n }\n\n int len = 0; \n\n for(int i = 1; i < 100000; i++) {\n if(num[i]) {\n if(i * 2 > 100000 || len == n/2) {\n // cout << i << endl;\n return empty ;\n } \n int left = n/2 - len;\n if(left < num[i]) {\n cout << \"DM\";\n return empty ;\n }\n len+=num[i] ;\n num[i * 2]-=num[i];\n for(int t = 0 ; t < num[i]; t++) ans.push_back(i); \n }\n }\n for(int i = 0 ; i < num[0]/2;i++) ans.push_back(0);\n return ans;\n }\n};",
"memory": "121503"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n sort(changed.begin(), changed.end());\n vector<int> m;\n int i = 0;\n for (int n: changed) {\n if (m.size() == i) {\n m.emplace_back(n);\n } else if (m[i] * 2 == n) {\n ++i;\n } else if (m[i] * 2 > n) {\n m.emplace_back(n);\n } else {\n return {};\n }\n }\n if (m.size() * 2 == changed.size()) {\n return m;\n } else {\n return {};\n }\n }\n};",
"memory": "122504"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n priority_queue<int, vector<int>, greater<int>> minH;\n sort(changed.begin(),changed.end());\n\n minH.push(changed[0]);\n vector<int> ans;\n\n for(int i=1;i<changed.size();i++){\n if(minH.empty()){\n minH.push(changed[i]);\n }\n else if(changed[i] == 2*minH.top()){\n // minH.pop();\n ans.push_back(minH.top());\n minH.pop();\n }\n else if(changed[i] < 2*minH.top()){\n minH.push(changed[i]);\n }\n else{\n return {};\n }\n }\n if(minH.empty())\n return ans;\n else\n return {};\n }\n};",
"memory": "122504"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "\nclass Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n % 2) return {};\n int MX = *max_element(changed.begin(), changed.end());\n vector<int> count(1+MX);\n for(auto c : changed) count[c]++;\n vector<int> ans;\n while(count[0]%2 == 0 && count[0] > 0) {\n ans.push_back(0);\n count[0]-=2;\n }\n for(int i = 1; i <= MX/2; i++) {\n while(count[i] > 0) {\n if(2 * i <= MX && count[2*i] > 0) {\n ans.push_back(i);\n count[i]--;\n count[2*i]--;\n } else {\n return {};\n }\n }\n }\n if(ans.size() != n/2) return {};\n return ans;\n }\n};",
"memory": "123505"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "/*\nApproach: normal Sort\nWe need to pair up elements in the array as explained above. Thus, the first validation we should do is, if the array size is even or not. If it's not we cannot have an answer.\nWe need to find the smallest element at each iteration. Hence, we can prepare a fred array \nusing counting sort, then iterating from left to right will provide us with the smallest \nelement. Once we got the smallest element say X we need to check if the element 2X exists \nin the array or not. If the element 2X is present in the freq array we will decrement its \ncount else the element X cannot be paired with any other element and hence we return an \nempty array.\nT.C. = O(n+k) where n is no of elements in changed and k is size of counting sort array\nS.C. O(k)\n\nCounting sort solution\nSimilar to the previous approach, we will find the smallest element always. \nThe only difference here is that instead of sorting the original array using built-in sorting functions we will use counting sort. We will use an array freq to store the frequency of each element in the given array.\nNow, we will iterate from 0 to the maximum value that is present in the array. For each element num we will follow the exact same process as we did previously, \nwe check for the element 2 * num and proceed accordingly.*/\n\nclass Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int maxEle = INT_MIN, size = changed.size();\n \n for(int& ele:changed)\n {\n maxEle = max(maxEle, ele);\n }\n vector<int> freq(maxEle+1, 0);\n \n for(int& ele:changed)\n {\n freq[ele]++;\n }\n vector<int> original;\n \n for(int num = 0;num<freq.size();num++)\n {\n if(freq[num]>0)\n {\n freq[num]--;\n if(2*num < freq.size() && freq[2*num]>0)\n {\n freq[2*num]--;\n original.push_back(num);\n num--;\n }\n else\n {\n return {};\n }\n }\n }\n return original;\n }\n};",
"memory": "123505"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int>findOriginalArray(vector<int>&changed) {\n int n = changed.size();\n vector<int>a,visited(n,0); \n\t\t// Sorting the array is important in this approach\n sort(changed.begin(), changed.end());\n\t\t\n int i=0,j=1;\n while((i<n && j < n)){\n if(visited[i] == 1){ // Keeping a track of all elements\n i++; // make sure these elements are no picked up again\n if(i==j) // jth index is for finding the double value of ith index element\n j++;\n continue;\n }\n if(visited[j] == 1){ \n j++;\n continue;\n }\n\t\t if(changed[j] == 2*changed[i]){ // If the doubled element is found then push back the element \n a.push_back(changed[i]);\n visited[i]++; // Mark the element in Visited array\n visited[j]++;\n i++;\n j++;\n }\n else{ // If element not found increment the jth index\n j++;\n }\n }\n \n if(2*a.size() == changed.size()) // array obtained should be of exactly half size that of Changed array\n return a;\n return {};\n }\n};",
"memory": "124506"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int left=0;\n int right=1;\n sort(changed.begin(),changed.end());\n vector<int> f;\n if(changed.size()%2==1){\n return f;\n }\n int n=changed.size();\n f=vector<int>(n);\n vector<int> e;\n // int count=0;\n while(right<n and left<n){\n if(f[left]==1){\n left++;\n continue;\n }\n f[left]=1;\n right=max(right,left+1);\n // count++;\n while((right<n and changed[left]*2!=changed[right]) or (right<n and f[right]==1)){\n right++;\n }\n if(right<n){\n f[right]=1;\n // cout<<left<<\" \";\n e.push_back(changed[left]);\n // count++;\n }\n else{\n break;\n }\n }\n // for(int i=0;i<e.size();i++){\n // cout<<e[i]<<\" \";\n // }\n if(e.size()==n/2) return e;\n else return vector<int>();\n\n \n }\n};",
"memory": "124506"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": " \nclass Solution {\npublic:\n vector<int>findOriginalArray(vector<int>&changed) {\n int n = changed.size();\n vector<int>a,visited(n,0); \n\t\t// Sorting the array is important in this approach\n sort(changed.begin(), changed.end());\n\t\t\n int i=0,j=1;\n while((i<n && j < n)){\n if(visited[i] == 1){ // Keeping a track of all elements\n i++; // make sure these elements are no picked up again\n if(i==j) // jth index is for finding the double value of ith index element\n j++;\n continue;\n }\n if(visited[j] == 1){ \n j++;\n continue;\n }\n\t\t if(changed[j] == 2*changed[i]){ // If the doubled element is found then push back the element \n a.push_back(changed[i]);\n visited[i]++; // Mark the element in Visited array\n visited[j]++;\n i++;\n j++;\n }\n else{ // If element not found increment the jth index\n j++;\n }\n }\n \n if(2*a.size() == changed.size()) // array obtained should be of exactly half size that of Changed array\n return a;\n return {};\n }\n};",
"memory": "125508"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n sort(changed.begin(), changed.end());\n int n=changed.size();\n vector<int> z = vector<int>(n, 1);\n bool hasAns=true;\n vector<int> ans;\n int ptr=0;\n for (int i=0; i<n; i++){\n if (z[i]){\n ans.push_back(changed[i]);\n z[i]=0;\n int a2=changed[i]*2;\n while (ptr<n){\n if (!z[ptr]){\n ptr++;\n }else{\n if (changed[ptr]<a2){\n ptr++;\n }else if(changed[ptr]==a2){\n z[ptr]=0;\n break;\n }else{\n ptr=n;\n break;\n }\n }\n }\n if (ptr==n){\n hasAns = false;\n break;\n }\n }\n }\n if (hasAns){\n return ans;\n }else{\n vector<int> empty;\n return empty;\n }\n }\n};",
"memory": "125508"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n sort(changed.begin(), changed.end());\n int maxi = -1;\n for(auto change: changed){\n maxi = max(change, maxi);\n }\n vector<int> freq(maxi+1, 0);\n for(int x=0;x<n;x++){\n freq[changed[x]]++;\n }\n vector<int> ans;\n for(auto change: changed){\n if(change*2<=maxi){\n if(freq[change]>0 and freq[change*2]>0){\n if(change == 0){\n if(freq[change]%2!=0){\n return {};\n }\n }\n freq[change]--;\n freq[change*2]--;\n ans.push_back(change);\n }\n }\n }\n if(ans.size()*2 != n){\n return {};\n }\n return ans;\n }\n};",
"memory": "126509"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // It can't be doubled array, if the size is odd\n if (changed.size() % 2) {\n return {};\n }\n \n int maxNum = 0;\n // Find the max element in the array\n for (int num : changed) {\n maxNum = max(maxNum, num);\n }\n \n vector<int> freq(2 * maxNum + 1, 0);\n // Store the frequency in the map\n for (int num : changed) {\n freq[num]++;\n }\n \n vector<int> original;\n for (int num = 0; num <= maxNum; num++) {\n // If element exists\n if (freq[num]) {\n freq[num]--;\n \n int twiceNum = num * 2;\n if (freq[twiceNum] > 0) {\n // Pair up the elements, decrement the count\n freq[twiceNum]--;\n // Add the original number to answer\n original.push_back(num);\n num--;\n } else {\n return {};\n }\n }\n }\n \n return original;\n }\n};",
"memory": "126509"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // It can't be doubled array, if the size is odd\n if (changed.size() % 2) {\n return {};\n }\n \n int maxNum = 0;\n // Find the max element in the array\n for (int num : changed) {\n maxNum = max(maxNum, num);\n }\n \n vector<int> freq(2 * maxNum + 1, 0);\n // Store the frequency in the map\n for (int num : changed) {\n freq[num]++;\n }\n \n vector<int> original;\n for (int num = 0; num <= maxNum; num++) {\n // If element exists\n if (freq[num]) {\n freq[num]--;\n \n int twiceNum = num * 2;\n if (freq[twiceNum] > 0) {\n // Pair up the elements, decrement the count\n freq[twiceNum]--;\n // Add the original number to answer\n original.push_back(num);\n num--;\n } else {\n return {};\n }\n }\n }\n \n return original;\n }\n};",
"memory": "127510"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() % 2) return {};\n \n int max_num = changed[max_element(changed.begin(), changed.end()) - changed.begin()];\n vector<int> freq(2 * max_num + 1, 0);\n \n for(int i : changed){\n freq[i]++;\n }\n\n vector<int> ans;\n for(int num = 0; num <= max_num; num++){\n if (!freq[num]) continue;\n\n freq[num]--;\n if (freq[num * 2] > 0){\n freq[num * 2]--;\n ans.push_back(num);\n num--;\n }\n else return {};\n }\n return ans;\n\n\n }\n};",
"memory": "127510"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() % 2) return {};\n \n int max_num = changed[max_element(changed.begin(), changed.end()) - changed.begin()];\n vector<int> freq(2 * max_num + 1, 0);\n \n for(int i : changed){\n freq[i]++;\n }\n\n vector<int> ans;\n for(int num = 0; num <= max_num; num++){\n if (!freq[num]) continue;\n\n freq[num]--;\n if (freq[num * 2] > 0){\n freq[num * 2]--;\n ans.push_back(num);\n num--;\n }\n else return {};\n }\n return ans;\n\n\n }\n};",
"memory": "128511"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n%2)\n return {};\n\n int N = 0;\n\n for(auto val:changed){\n N = max(N,val);\n }\n\n vector<int> ans;\n //unordered_map<int,int> mp;\n vector<int> mp(2*N + 1,0);\n\n for(auto val:changed){\n mp[val]++;\n }\n\n sort(changed.begin(),changed.end());\n\n for(auto val:changed){\n if(mp[val]>0){\n mp[val]--;\n if( mp[2*val]<=0){\n return {};\n }\n \n mp[2*val]--;\n ans.push_back(val);\n }\n }\n return ans;\n }\n};",
"memory": "128511"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> ans;\n queue<int> q;\n\n ranges::sort(changed);\n\n for (const int num : changed)\n if (!q.empty() && num == q.front()) {\n q.pop();\n } else {\n q.push(num * 2);\n ans.push_back(num);\n }\n\n return q.empty() ? ans : vector<int>();\n }\n};",
"memory": "129513"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int> &changed) {\n queue<int> q;\n vector<int> result;\n sort(changed.begin(), changed.end());\n\t\t\n for (int &i : changed) {\n if (q.front() != i) {\n result.push_back(i);\n q.push(i * 2);\n }\n else\n q.pop();\n }\n \n if (!q.empty())\n return {};\n return result;\n }\n};",
"memory": "130514"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int>ans;\n queue<int>q;\n\n ranges::sort(changed);\n\n for(const int num:changed){\n if(!q.empty() && num==q.front()){\n q.pop();\n }else{ \n q.push(num*2);\n ans.push_back(num); \n } \n } \n return q.empty()?ans:vector<int>(); \n }\n};",
"memory": "131515"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int> &changed) {\n queue<int> q;\n vector<int> result;\n sort(changed.begin(), changed.end());\n\t\t\n for (int &i : changed) {\n if (q.front() != i) {\n result.push_back(i);\n q.push(i * 2);\n }\n else\n q.pop();\n }\n \n if (!q.empty())\n return {};\n return result;\n }\n};",
"memory": "131515"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int size = changed.size();\n if(size&1){\n return {};\n }\n sort(changed.begin(), changed.end());\n unordered_map<int, int> mp;\n auto countZero = 0;\n vector<int> res;\n for(auto n : changed){\n if(n & 1){\n mp[n]+=1;\n }else if(n==0){\n countZero++;\n }\n else{\n auto half = n/2;\n if(mp.find(half) == mp.end() || mp[half]==0){\n mp[n]+=1;\n }else{\n mp[half] -= 1;\n res.push_back(half);\n }\n }\n }\n\n if(countZero %2==0 && res.size() + countZero/2 == size/2){\n res.insert(res.end(), countZero/2, 0);\n return res;\n }\n return {};\n }\n};",
"memory": "132516"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int size = changed.size();\n if(size&1){\n return {};\n }\n sort(changed.begin(), changed.end());\n unordered_map<int, int> mp;\n vector<int> res;\n for(auto n : changed){\n if(n & 1){\n mp[n]+=1;\n }\n else{\n auto half = n/2;\n if(mp.find(half) == mp.end() || mp[half]==0){\n mp[n]+=1;\n }else{\n mp[half] -= 1;\n res.push_back(half);\n }\n }\n }\n\n if(res.size() == size/2){\n return res;\n }\n return {};\n }\n};",
"memory": "133518"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& v) {\n int n=v.size();\n if(n%2==1) return {};\n\n vector<int> ans;\n sort(v.begin(),v.end());\n unordered_map<int,int> m;\n\n for(auto it:v){\n if(it%2==0 && m.find(it/2)!=m.end()){\n ans.push_back(it/2);\n m[it/2]--;\n\n if(m[it/2]==0) m.erase(it/2);\n }\n else{\n if(m.find(it)==m.end()){\n m[it]=0;\n }\n m[it]++;\n }\n }\n\n if(ans.size()==n/2) return ans;\n return {};\n }\n};",
"memory": "134519"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& arr)\n {\n // sort the array\n // for every element after the first one\n // we don't know if it's part of the original or part of changed\n \n // if half of its value has occurred before then decrement count\n \n std::sort(arr.begin(), arr.end());\n unordered_map<int,int> counts;\n \n int n = arr.size();\n vector<int> ans;\n for (auto x : arr)\n {\n if (x % 2 == 0 && counts.count(x/2))\n {\n counts[x/2]--;\n if (counts[x/2] == 0)\n {\n counts.erase(x/2);\n }\n ans.push_back(x/2);\n }\n else\n {\n counts[x]++;\n }\n }\n \n if (counts.size() == 0)\n {\n return ans;\n }\n else\n {\n return {};\n }\n }\n};",
"memory": "135520"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector <int> res;\n sort(changed.begin() , changed.end());\n unordered_map <int , int> freq;\n for(int i = changed.size() - 1 ; i >= 0 ; i--){\n if(freq.count(changed[i] * 2)){\n freq[changed[i] * 2]--;\n if(freq[changed[i] * 2] == 0)\n freq.erase(changed[i] * 2);\n res.push_back(changed[i]);\n }else{\n freq[changed[i]]++;\n }\n }\n if(freq.size()){\n return {};\n }\n return res;\n }\n};",
"memory": "136521"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& ch) {\n \n map<int,int>m;\n vector<int>ans;\n if(ch.size()%2==1) {\n return ans;\n }\n sort(ch.begin(),ch.end());\n for(int i=0;i<ch.size();i++) {\n if(!m.count(ch[i]) || m[ch[i]]==0) {\n int val = 2*ch[i];\n m[val]++;\n }\n else {\n ans.push_back(ch[i]/2);\n m[ch[i]]--;\n }\n \n }\n if(ans.size()*2 ==ch.size())\n return ans;\n\n vector<int>d;\n return d;\n \n }\n};",
"memory": "137523"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if (n & 1) return {};\n priority_queue<int, vector<int>, greater<int>> pq;\n for(int num: changed) {\n pq.push(num);\n }\n queue<int> doubled;\n vector<int> ans;\n while(!pq.empty()) {\n int e = pq.top();\n pq.pop();\n if (!doubled.empty() && doubled.front() == e) {\n doubled.pop();\n } else {\n ans.push_back(e);\n doubled.push(2 * e);\n }\n }\n if (!doubled.empty())\n {\n return {};\n }\n return ans;\n }\n};",
"memory": "138524"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n \n vector<int> ans;\n unordered_map<int,int> ump;\n int n=changed.size();\n if(n&1){\n return {};\n }\n sort(changed.begin(),changed.end());\n bool viszero=false;\n for(int i=0;i<n;i++){\n if(changed[i]==0 && viszero){\n ans.push_back(0);\n viszero=false;\n }\n else if(changed[i]==0 && !viszero){\n \n viszero=true;\n }\n else if(changed[i]%2==0 && ump[changed[i]/2]>0){\n ump[changed[i]/2]--;\n ans.push_back(changed[i]/2);\n }\n else{\n ump[changed[i]]++;\n }\n }\n if(ans.size()==n/2)\n return ans;\n else{\n return {};\n }\n }\n};",
"memory": "139525"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& arr) {\n int n = arr.size();\n unordered_map<int,int>mp;\n sort(arr.begin(),arr.end());\n vector<int>ans;\n for(int i = 0; i < n; i++){\n // cout << arr[i]/2<<endl;\n if( arr[i]%2 == 0 && mp[(arr[i]/2)]>0){\n mp[arr[i]/2]--;\n ans.push_back(arr[i]/2);\n // cout << 2 << \" \";\n }else{\n mp[arr[i]]++;\n // cout << arr[i]<< \" \"<<arr[i]/2<< \" \" << mp[arr[i]/2] << \" \" <<arr[i]*2 << \" \" << mp[arr[i]*2]<< endl;\n } \n }\n // cout << ans.size()<<endl;\n if(ans.size()*2 == arr.size()) return ans;\n ans.clear();\n return ans;\n }\n};",
"memory": "140526"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() % 2 != 0) return {};\n \n vector<int> ans;\n unordered_map<int, int> count;\n ans.reserve(changed.size() / 2);\n \n sort(changed.begin(), changed.end());\n\n for (int num : changed) {\n count[num]++;\n }\n\n for (int num : changed) {\n if (count[num] > 0) {\n if (count[num * 2] > 0) {\n ans.push_back(num);\n count[num]--;\n count[num * 2]--;\n } else {\n return {};\n }\n }\n }\n \n return ans;\n }\n};\n",
"memory": "141528"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n unordered_map<int, int> map;\n for(int i = 0; i < n; i++) map[changed[i]]++;\n vector<int> ret;\n ret.reserve(n/2);\n vector<int> failureRet;\n while(!map.empty()){\n int x = map.begin()->first;\n if(x == 0){\n if(map[0] & 1) return failureRet;\n ret.insert(ret.end(), map[0] / 2, 0);\n map.erase(0);\n continue;\n }\n while(!(x & 1) && map.count(x/2)) x /= 2;\n while(map.count(x)){\n if(!map.count(x*2) || map[x*2] < map[x]) return failureRet;\n ret.insert(ret.end(), map[x], x);\n map[x*2] -= map[x];\n map.erase(x);\n x *= 2;\n if(map[x] == 0) map.erase(x);\n }\n }\n return ret;\n }\n};",
"memory": "142529"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n unordered_map<int,int> mp;\n int n = changed.size();\n sort(changed.begin(),changed.end());\n vector<int> result;\n for(int i=0;i<n;i++){\n int num = changed[i];\n if((num % 2 == 0) && (mp.find(num/2) != mp.end())){\n mp[num/2]--;\n if(mp[num/2] == 0) mp.erase(num/2);\n }\n else{\n mp[num]++;\n result.push_back(num);\n }\n }\n for(auto &it : mp){\n cout<<\"key-> \"<<it.first<<\"value-> \"<<it.second<<endl;\n }\n if(mp.size() != 0) return {};\n return result;\n }\n};",
"memory": "143530"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n unordered_map<int,int> mp;\n int n = changed.size();\n if(n % 2 != 0) return {};\n sort(changed.begin(),changed.end());\n vector<int> result;\n for(int i=0;i<n;i++){\n int num = changed[i];\n if((num % 2 == 0) && (mp.find(num/2) != mp.end())){\n mp[num/2]--;\n if(mp[num/2] == 0) mp.erase(num/2);\n }\n else{\n mp[num]++;\n result.push_back(num);\n }\n }\n if(mp.size() != 0) return {};\n return result;\n }\n};",
"memory": "144531"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // Sort changed in reverse order.\n sort(changed.begin(), changed.end(), greater<int>());\n\n // The original array.\n vector<int> original;\n\n // These numbers must exist.\n multiset<int> must_exist;\n\n for (int num : changed) {\n if (must_exist.contains(num)) {\n must_exist.erase(must_exist.lower_bound(num));\n original.push_back(num);\n continue;\n }\n // If num is odd, it must have been doubled.\n // But, we didn't find a doubled element.\n if (num % 2 != 0) {\n return vector<int>();\n } else {\n must_exist.insert(num / 2);\n }\n }\n\n if (must_exist.empty()) {\n return original;\n } else {\n return vector<int>();\n }\n }\n};",
"memory": "144531"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n&1) return {};\n\n vector<int> res;\n unordered_map<int,int> freq;\n for(int it : changed)\n freq[it]++;\n\n sort(changed.begin(),changed.end());\n\n for(int i=0;i<n;i++){\n int val = changed[i];\n if(freq[val]>0){\n if(freq[2*val]>0){\n freq[val]--;\n freq[2*val]--;\n res.push_back(val);\n }\n else\n return {};\n }\n }\n\n return res;\n }\n};",
"memory": "145533"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "#include <vector>\n#include <set>\n#include <algorithm>\n\nclass Solution {\npublic:\n #include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nstd::vector<int> findOriginalArray(std::vector<int>& d) {\n std::unordered_map<int, int> count;\n std::vector<int> ans;\n\n // Count the frequency of each number\n for (int num : d) {\n count[num]++;\n }\n\n // Sort the array to handle the smallest numbers first\n std::sort(d.begin(), d.end());\n\n // Iterate through the sorted array\n for (int num : d) {\n if (count[num] == 0) {\n continue;\n }\n\n if (count[2 * num] == 0) {\n return {}; \n }\n\n ans.push_back(num);\n count[num]--;\n count[2 * num]--;\n }\n\n return ans.size() * 2 == d.size() ? ans : std::vector<int>{};\n}\n\n};\n",
"memory": "145533"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> emptyVec;\n if (changed.size() % 2 == 1) return emptyVec;\n unordered_map<int, int> freq;\n for (int i = 0; i < changed.size(); i++) {\n freq[changed[i]]++;\n }\n vector<int> res;\n for (int i = 0; i < changed.size(); i++) {\n if (changed[i] == 0) {\n if (freq[0] < 2) continue;\n freq[0] -= 2;\n res.push_back(0);\n } else {\n if (freq[changed[i]] != 0) {\n int current = changed[i];\n while (current % 2 != 1) current /= 2;\n while (current <= 100000) {\n while (freq[current] > 0) {\n if (freq[current * 2] <= 0) return emptyVec;\n res.push_back(current);\n freq[current]--;\n freq[current * 2]--;\n }\n current *= 2;\n }\n } else continue;\n }\n }\n if (res.size() != changed.size() / 2) return emptyVec;\n return res;\n }\n};",
"memory": "146534"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& v) {\n \n int n = v.size() , count =0;\n if(n % 2) return {};\n vector<int> res;\n sort(v.begin(),v.end());\n unordered_map<int,int> m;\n for(auto x:v) m[x]++;\n for(auto x:v) {\n\n double val = x*2;\n if(val == 0 && m[0] > 1) {\n \n res.push_back(0);\n m[0]-=2;\n count++;\n continue;\n }\n else if(val != 0 and m.find(val)!=m.end() and m[x]>0 and m[val]>0){\n\n \n count++;\n // cout<<val<<\" \"<<x<<endl;\n res.push_back(x);\n m[x]--;\n m[val]--;\n \n }\n if(count == n/2) return res;\n }\n return {};\n\n\n }\n};",
"memory": "146534"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n\n if(changed.size()%2!=0)\n return {};\n\n vector<int>ans;\n unordered_map<int,int>mp;\n unordered_map<int,int>used;\n\n sort(changed.begin(),changed.end());\n for(int i=0;i<changed.size();i++)\n {\n mp[changed[i]]++;\n }\n\n\n\n for(int i=changed.size()-1;i>=0;i--){\n \n // cout<<changed[i]<<\" \";\n if(changed[i]==0){\n\n if(mp[changed[i]]>1){\n\n mp[changed[i]]-=2;\n ans.push_back(changed[i]);\n }\n }\n else if(changed[i]%2==0){\n int val = changed[i]/2;\n if(mp[changed[i]]>0 && mp[val]>0 && mp.find(val)!=mp.end()){\n // cout<<changed[i]<<\" \";\n // cout<<changed[i]/2<<\" \";\n ans.push_back(val);\n mp[val]--;\n mp[changed[i]]--;\n\n // cout<<mp[changed[i]/2];\n }\n \n }\n \n }\n\n if(ans.size()==changed.size()/2)\n return ans;\n\n return {};\n \n }\n};",
"memory": "147535"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> ans;\n int n = changed.size();\n if (n % 2 != 0) return {};\n\n map<int, int> mp;\n for (int i = 0; i < n; i++){\n mp[changed[i]]++;\n }\n\n for (auto &p : mp){\n // cout << p.first << \"==\" << p.second << endl;\n if (p.first == 0){\n if (p.second % 2 == 0){\n ans.insert(ans.end(), p.second / 2, 0);\n p.second = 0;\n }\n else return {};\n }\n else if (p.second == 0) continue;\n else if (mp.count(p.first * 2)){\n if (mp[p.first * 2] > 0 && mp[p.first] > 0){\n int mn = min(p.second, mp[p.first * 2]);\n if (mn != p.second) return {};\n ans.insert(ans.end(), mn, p.first);\n mp[p.first * 2] -= mn;\n mp[p.first] -= mn;\n // cout << \"done\" << endl;\n }\n else continue;\n }\n else return {};\n if (ans.size() == n / 2) return ans;\n }\n return ans;\n }\n};",
"memory": "147535"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n=changed.size();\n if(n%2!=0){\n return {};\n }\n sort(changed.begin(),changed.end());\n map<int,int>mp;\n vector<int>result;\n for(auto &num:changed){\n mp[num]++;\n\n\n }\n for(int &num:changed){\n int twice=2*num;\n if(mp[num]==0)continue;\n\n if(mp.find(twice)==mp.end() || mp[twice]==0 ){\n return {};\n }\n result.push_back(num);\n mp[num]--;\n mp[twice]--;\n }\n return result;\n \n }\n};",
"memory": "148536"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n=changed.size();\n if(n%2==1){\n return {};\n }\n sort(changed.begin(),changed.end());\n vector<int>ans;\n map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[changed[i]]++;\n }\n for(int i=0;i<n;i++){\n if(mp[changed[i]]==0)continue;\n if(mp[changed[i]*2]==0)return {};\n ans.push_back(changed[i]);\n mp[changed[i]]--;\n mp[changed[i]*2]--;\n }\n return ans;\n }\n};",
"memory": "148536"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size(); \n if(n == 0 || n % 2)\n return vector<int>();\n\n map<int,int> M;\n for(int num : changed)\n M[num]++;\n\n vector<int> result;\n for(auto& it : M) {\n if(it.second == 0)\n continue;\n if(it.first == 0) {\n if(it.second % 2)\n return vector<int>();\n for(int i = 0; i < it.second/2; ++i)\n result.push_back(0);\n it.second = 0;\n continue;\n }\n\n int tgt = 2 * it.first, cnt = it.second;\n if(M.count(tgt) && M[tgt] >= cnt) {\n for(int i = 0; i < it.second; ++i)\n result.push_back(it.first);\n M[tgt] -= cnt;\n M[it.first] = 0;\n }\n }\n\n for(const auto& it : M) {\n if(it.second)\n return vector<int>();\n }\n\n return result;\n }\n};",
"memory": "149538"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n sort(changed.begin(),changed.end());\n map<int,int>hash;\n vector<int>res;\n for(auto &it:changed)\n {\n if(it%2==0)\n {\n if(hash.find(it/2)!=hash.end())\n {\n hash[it/2]--;\n res.push_back(it/2);\n if(hash[it/2]==0) hash.erase(it/2);\n }\n else\n hash[it]++;\n }\n else hash[it]++;\n }\n if(hash.empty()) return res;\n return {};\n }\n};",
"memory": "149538"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n map<int, int> freq;\n for (auto x : changed) {\n freq[x]++;\n }\n\n vector<int> ans;\n bool doubled = changed.size() % 2 == 0;\n for (auto& x : freq) {\n if (!x.second) continue;\n if (x.first == 0) {\n if (x.second % 2) {\n doubled = false;\n break;\n }\n \n if (doubled) {\n for (int i = 0; i < x.second / 2; i++) {\n ans.push_back(x.first);\n }\n }\n } else {\n for (int i = 0; i < x.second; i++) {\n ans.push_back(x.first);\n }\n\n freq[x.first * 2] -= x.second; \n x.second = 0;\n if (freq[x.first * 2] < 0) {\n doubled = false;\n break;\n } \n }\n } \n \n if (doubled) {\n return ans;\n } else {\n return {};\n }\n }\n};",
"memory": "150539"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() % 2 != 0) return {};\n unordered_map<int, int> count;\n for (int num : changed) count[num]++;\n vector<int> keys;\n for (auto& [key, _] : count) keys.push_back(key);\n sort(keys.begin(), keys.end());\n vector<int> original;\n for (int x : keys) {\n if (count[x] > count[2 * x]) return {};\n for (int i = 0; i < count[x]; ++i) {\n original.push_back(x);\n count[2 * x]--;\n }\n }\n return original;\n }\n};",
"memory": "150539"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> ans;\n if(changed.size()%2)\n return ans;\n sort(changed.begin(), changed.end());\n bool idChanged=true;\n map<int, bool> isDeleted;\n int idx;\n for(int i=0; i<changed.size(); ++i)\n {\n // cout<<changed[i]<<\" ->\"<<isDeleted[i]<<\" \";\n if(!(isDeleted[i]))\n {\n auto it=lower_bound(changed.begin()+i+1, changed.end(), 2*changed[i]);\n if(it==changed.end())\n {\n ans.clear();\n return ans;\n }\n\n idx=it-changed.begin();\n while(idx<changed.size() && isDeleted[idx])\n {\n ++idx;\n }\n if(idx>=changed.size() || changed[idx]!=(2*changed[i]))\n {\n ans.clear();\n return ans;\n }\n // cout<<idx<<\" \";\n isDeleted[idx]=true;\n ans.push_back(changed[i]);\n }\n // cout<<endl;\n }\n return ans;\n }\n};",
"memory": "151540"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> ans;\n if(changed.size()%2)\n return ans;\n sort(changed.begin(), changed.end());\n // bool idChanged=true;\n map<int, bool> isDeleted;\n int idx;\n for(int i=0; i<changed.size(); ++i)\n {\n // cout<<changed[i]<<\" ->\"<<isDeleted[i]<<\" \";\n if(!(isDeleted[i]))\n {\n auto it=lower_bound(changed.begin()+i+1, changed.end(), 2*changed[i]);\n if(it==changed.end())\n {\n ans.clear();\n return ans;\n }\n\n idx=it-changed.begin();\n while(idx<changed.size() && isDeleted[idx])\n {\n ++idx;\n }\n if(idx>=changed.size() || changed[idx]!=(2*changed[i]))\n {\n ans.clear();\n return ans;\n }\n // cout<<idx<<\" \";\n isDeleted[idx]=true;\n ans.push_back(changed[i]);\n }\n // cout<<endl;\n }\n return ans;\n }\n};",
"memory": "152541"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution\n{\npublic:\n vector<int> findOriginalArray(vector<int> &changed)\n {\n int n = changed.size();\n sort(changed.rbegin(),changed.rend());\n if(n%2==0)\n {\n map<int, int> mpp;\n vector<int> v;\n for (int i = 0; i < n; i++)\n {\n mpp[changed[i]]++;\n }\n for (int i=0;i<n;i++)\n {\n int f = changed[i];\n if (f % 2 == 0 && f!=0)\n {\n if (mpp.find(f) != mpp.end() && mpp.find(f / 2) != mpp.end())\n {\n v.push_back(f / 2);\n mpp[f]--;\n if (mpp[f] == 0)\n {\n mpp.erase(f);\n }\n mpp[f / 2]--;\n if (mpp[f / 2] == 0)\n {\n mpp.erase(f/2);\n }\n }\n else if (mpp.find(f) != mpp.end() && mpp.find(2 * f) != mpp.end())\n {\n v.push_back(f);\n mpp[f]--;\n if (mpp[f] == 0)\n {\n mpp.erase(f);\n }\n mpp[2 * f]--;\n if (mpp[2 * f] == 0)\n {\n mpp.erase(2 * f);\n }\n }\n }\n else if(f%2==0 && f==0)\n {\n if(mpp[f]>=2)\n {\n v.push_back(f);\n mpp[f]--;\n mpp[f]--;\n if(mpp[f]==0)\n {\n mpp.erase(f);\n }\n }\n }\n else\n {\n\n if (mpp.find(f) != mpp.end() && mpp.find(2 * f) != mpp.end())\n {\n v.push_back(f);\n mpp[f]--;\n if (mpp[f] == 0)\n {\n mpp.erase(f);\n }\n mpp[2 * f]--;\n if (mpp[2 * f] == 0)\n {\n mpp.erase(2 * f);\n }\n }\n }\n }\n if (v.size() == n / 2)\n return v;\n else\n return {};\n }\n else\n {\n return {};\n }\n }\n};",
"memory": "152541"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n\n if(n%2) {\n return vector<int>();\n }\n\n vector<int> v;\n\n map<int, int> m;\n\n for(int i=0; i<n; ++i) {\n ++m[changed[i]];\n }\n\n for(auto i : m) {\n if(i.first == 0) {\n if(i.second%2)\n return vector<int>();\n m[0]/=2;\n }\n\n else if(m.find(2*i.first)!=m.end()) {\n m[2*i.first]-=m[i.first];\n }\n }\n\n for(auto i : m) {\n if(i.second>0) {\n while(m[i.first]--) {\n v.push_back(i.first);\n }\n }\n }\n\n if(v.size()*2 != n) {\n return vector<int>();\n }\n\n return v;\n }\n};",
"memory": "153543"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& nums) {\n int n = nums.size();\n\n if (n % 2 )\n return {};\n unordered_map<int,int>um;\n \n sort(nums.begin(), nums.end());\n \n for (int i=0; i<nums.size(); i++)\n {\n um[nums[i]]++;\n }\n\n int count = 0;\n vector<int>ans;\n for (int i=0; i<n; i++)\n {\n \n int a = nums[i];\n int b = 2 * nums[i];\n\n //if ()\n if (um[a] <= 0)\n continue;\n if (count * 2 == n)\n return ans;\n if (um[b] > 0)\n {\n ans.push_back(a);\n um[a]--;\n if (um[b] <= 0)\n return {};\n um[b]--;\n count++;\n }\n \n }\n\n if (count * 2 == n)\n return ans;\n \n return {};\n \n }\n};",
"memory": "153543"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int len = changed.size();\n if (len % 2 == 1) {\n return {};\n }\n\n sort(changed.begin(), changed.end());\n\n unordered_map<int, int> cnt;\n for (int i: changed) {\n cout << i << \" \";\n cnt[i]++;\n }\n\n\n vector<int> original_array;\n for (int i : changed) {\n if (i == 0 and cnt[i] > 0) {\n if (cnt[i] % 2 == 0) {\n for (int x = 0; x < cnt[i] / 2; x++) {\n original_array.push_back(0);\n }\n cnt[i] = 0;\n } else {\n return vector<int>();\n }\n }\n cnt[i]--;\n if (cnt[i] >= 0 and cnt[2 * i] > 0) {\n original_array.push_back(i);\n cnt[2 * i]--;\n }\n }\n\n bool is_valid = original_array.size() == len / 2;\n\n return is_valid ? original_array : vector<int>();\n }\n};\n",
"memory": "154544"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int len = changed.size();\n if (len % 2 == 1) {\n return {};\n }\n\n sort(changed.begin(), changed.end());\n\n unordered_map<int, int> cnt;\n for (int i: changed) {\n cout << i << \" \";\n cnt[i]++;\n }\n\n\n vector<int> original_array;\n for (int i : changed) {\n if (i == 0 and cnt[i] >= 2) {\n original_array.push_back(i);\n cnt[i] -= 2;\n continue;\n }\n cnt[i]--;\n if (i != 0 and cnt[i] >= 0 and cnt[2 * i] > 0) {\n original_array.push_back(i);\n cnt[2 * i]--;\n }\n }\n\n bool is_valid = original_array.size() == len / 2;\n\n return is_valid ? original_array : vector<int>();\n }\n};\n",
"memory": "154544"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if(changed.size()%2==1){\n return {};\n }\n unordered_map<int,int> mp;\n vector<int> ans;\n int s = changed.size()/2;\n for(auto i:changed){\n mp[i]++;\n }\n sort(changed.begin(),changed.end());\n for(int i=0;i<changed.size();i++){\n if(changed[i]==0){\n if(mp[0]>=2){\n ans.push_back(0);\n mp[0]-=2;\n }\n continue;\n }\n if(mp[2*changed[i]]>=1 && mp[changed[i]]>=1){\n mp[changed[i]]--;\n mp[2*changed[i]]--;\n ans.push_back(changed[i]);\n //cout<<ans.back()<<\" \";\n if(ans.size()==s){\n return ans;\n }\n }\n }\n if(s==ans.size()){\n return ans;\n }\n return {};\n }\n};",
"memory": "155545"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& a) {\n int n = a.size();\n if(n % 2 == 1) return {};\n vector<int> v;\n map<int, int> m;\n sort(a.begin(), a.end());\n for(auto x : a) if(x % 2 == 0) m[x]++;\n for(int i = 0; i < n; i++){\n if(a[i] % 2 == 1){\n if(m[a[i] * 2] > 0){\n v.push_back(a[i]);\n m[a[i] * 2]--;\n }else return {};\n }else{\n if(a[i] == 0){\n if(m[0] % 2 == 1) return {};\n int s = m[0] / 2;\n while(s > 0){\n v.push_back(a[i]);\n s--;\n }\n m.erase(0);\n }else{\n while(m[a[i]] > 0 && m[a[i] * 2] > 0){\n v.push_back(a[i]);\n m[a[i] * 2]--;\n m[a[i]]--;\n } \n if(m[a[i]] > m[a[i] * 2]) return {};\n \n }\n }\n }\n return v;\n }\n};",
"memory": "156546"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& v) {\n \n vector<int> ans;\n if(v.size() % 2) return ans;\n map<int,int> m;\n for(int i=0;i<v.size();i++) m[v[i]]++;\n sort(v.begin(),v.end());\n for(int i=0;i<v.size();i++){\n\n if(m[v[i]] <=0) continue;\n long long val = v[i]*2;\n m[v[i]]--;\n if(m[val] > 0){\n ans.push_back(v[i]);\n m[val]--;\n }\n }\n if(ans.size() < v.size()/2) return {};\n return ans;\n\n\n }\n};",
"memory": "156546"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n sort(changed.rbegin(), changed.rend());\n unordered_map<int,int>mp;\n vector<int>ans;\n for(auto &i:changed){\n if(mp[2*i]>0){\n ans.push_back(i);\n mp[2*i]--;\n }\n else{\n mp[i]++;\n }\n }\n if(changed.size()%2!=0 || ans.size()!=changed.size()/2){\n return {};\n }\n return ans;\n }\n};",
"memory": "157548"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n vector<int> result;\n if (n%2!=0){\n return {};\n }\n unordered_map<int,int> freq;\n for (int i:changed){\n freq[i]++;\n }\n sort(changed.begin(), changed.end());\n for(int i:changed){\n if(freq[i]<=freq[i*2] && freq[i]!=0)\n {\n if(i==0)\n {\n if(freq[i]%2!=0)\n continue;\n else{\n for(int j=0;j<freq[i]/2;j++)\n {\n result.push_back(i);\n }\n freq[i]=0;\n }\n }\n for(int j=0;j<freq[i];j++)\n {\n result.push_back(i);\n }\n freq[i*2]-=freq[i];\n freq[i]=0; \n }\n }\n if (result.size()!=n/2){\n return {};\n }\n return result;\n }\n};",
"memory": "157548"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nmap<int,int> mp;\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> v;\n sort(changed.begin(), changed.end());\n for(int i=0; i<changed.size(); i++)\n {\n if(mp[changed[i]])\n {\n mp[changed[i]]--;\n }\n else\n {\n v.push_back(changed[i]);\n mp[2*changed[i]]++;\n }\n }\n for(auto element : mp)\n {\n if(element.second>0)\n {\n v.clear();\n }\n }\n return v;\n }\n};",
"memory": "158549"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& c) {\n int n = c.size();\n priority_queue <int, vector<int>, greater<int>> pq;\n map<int,int>m;\n for(int i=0;i<n;i++){\n pq.push(c[i]);\n m[c[i]]++;\n }\n \n vector<int>ans;\n while(!pq.empty()){\n int t = pq.top();\n pq.pop();\n if(m[t]>0){\n if(m[2*t]>0 && t!=0){\n ans.push_back(t);\n m[t]--;\n m[2*t]--;\n }\n else if(t==0 && m[t]>=2){\n ans.push_back(t);\n m[t]--;\n m[2*t]--;\n }\n }\n }\n if(n%2==1)return {};\n if(ans.size()!=(n/2)) return {};\n return ans;\n }\n};",
"memory": "163555"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n=changed.size();\n if(n%2==1)return {};\n sort(changed.begin(),changed.end());\n map<int,int> mp,vis;\n vector<int> ans;\n for(auto it:changed)mp[it]++;\n for(auto it:changed){\n if(vis.find(it)!=vis.end())continue;\n if(mp.find(2*it)!=mp.end() && vis.find(2*it)==vis.end()){\n ans.push_back(it);\n mp[it]--;\n mp[2*it]--;\n if(mp[it]==0){\n vis[it]=1;\n }\n if(mp[2*it]==0){\n vis[2*it]=1;\n }\n }\n }\n for(auto it:mp){\n if(it.second!=0)return {};\n }\n if(ans.size()!=n/2)return {};\n return ans;\n }\n};",
"memory": "164556"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> res;\n if(changed.size()%2==1) return res;\n unordered_map<int,int> m;\n for(auto num:changed){\n m[num]++;\n }\n set<int> s(changed.begin(),changed.end());\n for(auto num:s){\n if(m[num]==0) continue;\n if(!m.count(2*num)) return {};\n if(num==0){\n if(m[0]%2==1) return {};\n int cnt = m[0]/2;\n while(cnt--) res.push_back(0);\n m[0] = 0;\n }\n else {\n m[2*num]-=m[num];\n if(m[2*num]<0) return {};\n while(m[num]--) res.push_back(num);\n }\n }\n return res;\n }\n};",
"memory": "165558"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> res;\n if(changed.size()%2==1) return res;\n unordered_map<int,int> m;\n for(auto num:changed){\n m[num]++;\n }\n set<int> s(changed.begin(),changed.end());\n for(auto num:s){\n if(m[num]==0) continue;\n if(m[num]>m[2*num]) return {};\n if(num==0){\n if(m[0]%2==1) return {};\n int cnt = m[0]/2;\n while(cnt--) res.push_back(0);\n m[0] = 0;\n }\n else {\n m[2*num]-=m[num];\n while(m[num]--) res.push_back(num);\n }\n }\n return res;\n }\n};",
"memory": "166559"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> res;\n if(changed.size()%2==1) return res;\n unordered_map<int,int> m;\n for(auto num:changed){\n m[num]++;\n }\n set<int> s(changed.begin(),changed.end());\n for(auto num:s){\n if(m[num]==0) continue;\n if(m[num]>m[2*num]) return {};\n if(num==0){\n if(m[0]%2==1) return {};\n int cnt = m[0]/2;\n while(cnt--) res.push_back(0);\n }\n else {\n m[2*num]-=m[num];\n while(m[num]--) res.push_back(num);\n }\n }\n return res;\n }\n};",
"memory": "167560"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool canReorderDoubled(vector<int>& arr) {\n unordered_map<int,int>hash;\n sort(arr.begin(),arr.end());\n for(int i=0;i<arr.size();i++){\n hash[arr[i]]++;\n }\n for(int i=0;i<arr.size();i++)\n {\n if(hash[arr[i]]!=0 && hash[2*arr[i]]!=0){\n hash[arr[i]]--;\n hash[2*arr[i]]--;\n }\n }\n for(int i=0;i<arr.size();i++){\n if(hash[arr[i]]!=0){\n return false;\n }\n }\n return true;\n }\n vector<int> findOriginalArray(vector<int>& changed) {\n unordered_map<int,int>hash;\n for(int i=0;i<changed.size();i++){\n hash[changed[i]]++;\n }\n bool flag=canReorderDoubled(changed); \n \n vector<int>ans;\n if(flag){\n for(int i=0;i<changed.size();i++){\n if(hash[changed[i]]!=0 && hash[changed[i]*2]!=0){\n hash[changed[i]]--;\n hash[changed[i]*2]--;\n ans.push_back(changed[i]);\n \n }\n \n }\n return ans;\n }\n \n return {};\n }\n};",
"memory": "168561"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n%2!=0) return {};\n\n sort(changed.begin(), changed.end());\n multiset<int> seen;\n vector<int> res;\n for(int i=0; i<n; i++){\n int num = changed[i];\n if(seen.find(num)!=seen.end()) seen.erase(seen.find(num));\n else{\n res.push_back(num);\n seen.insert(num*2);\n }\n }\n if(seen.size()==0) return res;\n return {};\n }\n};",
"memory": "169563"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n%2!=0) return {};\n\n sort(changed.begin(), changed.end());\n multiset<int> seen;\n vector<int> res;\n for(int i=0; i<n; i++){\n int num = changed[i];\n if(seen.find(num)!=seen.end()){\n seen.erase(seen.find(num));\n }\n else{\n res.push_back(num);\n seen.insert(num*2);\n }\n }\n if(seen.size()==0) return res;\n return {};\n }\n};",
"memory": "170564"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> original;\n if(changed.size()%2)return original;\n\n unordered_map<int,int> occursChanged,occursorg;\n for(auto num:changed){\n occursChanged[num]+=1;\n }\n set<int> numSet(changed.begin(),changed.end());\n //start building from 1 -> 2, 2 -> 4\n for(auto num:numSet){\n //num is original for sure\n if(num%2 || !occursChanged[num/2]){\n //2*odd must be greater or equal to odd\n if(occursChanged[num * 2] < occursChanged[num])return original;\n occursorg[num] = occursChanged[num];\n }\n\n //may be a duplicate\n else{\n occursorg[num] = occursChanged[num] - occursorg[num/2];\n }\n \n }\n\n\n\n //push half the zeroes\n if(occursChanged[0]){\n original = vector<int>(occursChanged[0]/2);\n }\n\n for(auto num:numSet){\n if(!num)continue;\n for(int i = 0;i < occursorg[num];i++){\n original.push_back(num);\n }\n }\n return original;\n }\n};",
"memory": "171565"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n vector<int> original;\n if(changed.size()%2)return original;\n\n unordered_map<int,int> occursChanged,occursorg;\n for(auto num:changed){\n occursChanged[num]+=1;\n }\n set<int> numSet(changed.begin(),changed.end());\n for(auto num:numSet){\n\n //is original for sure\n if(num%2 || !occursChanged[num/2]){\n //2*odd must be greater or equal to odd\n if(occursChanged[num * 2] < occursChanged[num])return original;\n occursorg[num] = occursChanged[num];\n }\n\n //may have duplicates\n else{\n occursorg[num] = occursChanged[num] - occursorg[num/2];\n }\n }\n\n\n\n //push half the zeroes\n if(occursChanged[0]){\n original = vector<int>(occursChanged[0]/2);\n }\n\n for(auto num:numSet){\n if(!num)continue;\n for(int i = 0;i < occursorg[num];i++){\n original.push_back(num);\n }\n }\n return original;\n }\n};",
"memory": "172566"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& arr) {\n int n = arr.size();\n if(n % 2 == 1){\n return {};\n }\n\n sort(arr.begin(), arr.end());\n\n unordered_multiset<int> st;\n int zeros = 0;\n\n for(auto &x: arr){\n if(x > 0){\n st.insert(x);\n } else{\n zeros++;\n }\n }\n\n if(zeros % 2 == 1){\n return {};\n }\n\n int count = 0;\n // count double elements\n vector<int> ans;\n for(auto &x: arr){\n if(st.find(2*x) != st.end() and st.find(x) != st.end()){\n auto it = st.find(2*x);\n st.erase(it);\n it = st.find(x);\n st.erase(it);\n count++; // found 1\n ans.push_back(x);\n }\n }\n\n if(count*2 == 1 or count + (zeros/2) != n/2){\n return {};\n }\n\n // include zeros\n for(int i = 0; i < zeros/2; i++){\n ans.push_back(0);\n }\n\n return ans;\n\n }\n\n // handle zero diffrently\n};",
"memory": "173568"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if(changed.size()&1) return {};\n\n multiset<int> s;\n \n int countZeros = 0;\n for(auto &i: changed) {\n if(i==0) ++countZeros;\n else s.insert(i);\n }\n\n vector<int> ans;\n if(countZeros&1) return {};\n else ans.resize(countZeros>>1, 0);\n\n while(!s.empty()) {\n auto it_h = s.find((*begin(s))*2);\n if(it_h==end(s) || it_h==begin(s)) break;\n\n ans.push_back(*begin(s));\n s.erase(begin(s));\n s.erase(it_h);\n }\n\n if(s.empty()) return ans;\n return {};\n }\n};",
"memory": "174569"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n if (changed.size()%2==1) return {};\n \n int cnt=0;\n multiset<int> ms;\n vector<int> ans;\n for (int i=0;i<changed.size();++i){\n if (changed[i]==0) ++cnt;\n else ms.insert(changed[i]);\n }\n if (cnt%2==1) return {};\n else for (int i=0;i<cnt/2;++i) ans.push_back(0);\n int SIZE=ms.size();\n for (int i=0;i<SIZE/2;++i){\n if (ms.find(2*(*ms.begin()))!=ms.end()){\n ans.push_back(*ms.begin());\n ms.erase(ms.find(2*(*ms.begin())));\n ms.erase(ms.begin());\n }\n else return {};\n } \n return ans;\n }\n};",
"memory": "175570"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n if (changed.size()%2==1) return {};\n \n int cnt=0;\n multiset<int> ms;\n vector<int> ans;\n for (int i=0;i<changed.size();++i){\n if (changed[i]==0) ++cnt;\n else ms.insert(changed[i]);\n }\n if (cnt%2==1) return {};\n else for (int i=0;i<cnt/2;++i) ans.push_back(0);\n int SIZE=ms.size();\n for (int i=0;i<SIZE/2;++i){\n if (ms.find(2*(*ms.begin()))!=ms.end()){\n ans.push_back(*ms.begin());\n ms.erase(ms.find(2*(*ms.begin())));\n ms.erase(ms.begin());\n }\n else return {};\n } \n return ans;\n }\n};",
"memory": "176571"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n if (changed.size()%2==1) return {};\n \n int cnt=0;\n multiset<int> ms;\n vector<int> ans;\n for (int i=0;i<changed.size();++i){\n if (changed[i]==0) ++cnt;\n else ms.insert(changed[i]);\n }\n if (cnt%2==1) return {};\n else for (int i=0;i<cnt/2;++i) ans.push_back(0);\n int SIZE=ms.size();\n for (int i=0;i<SIZE/2;++i){\n if (ms.find(2*(*ms.begin()))!=ms.end()){\n ans.push_back(*ms.begin());\n ms.erase(ms.find(2*(*ms.begin())));\n ms.erase(ms.begin());\n }\n else return {};\n } \n return ans;\n }\n};",
"memory": "176571"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n if (changed.size()%2==1) return {};\n \n int cnt=0;\n multiset<int> ms;\n vector<int> ans;\n for (int i=0;i<changed.size();++i){\n if (changed[i]==0) ++cnt;\n else ms.insert(changed[i]);\n }\n if (cnt%2==1) return {};\n else for (int i=0;i<cnt/2;++i) ans.push_back(0);\n while (ms.size()>0){\n if (ms.find(2*(*ms.begin()))!=ms.end()){\n ans.push_back(*ms.begin());\n ms.erase(ms.find(2*(*ms.begin())));\n ms.erase(ms.begin());\n }\n else return {};\n } \n return ans;\n }\n};",
"memory": "177573"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n if (changed.size()%2==1) return {};\n \n int cnt=0;\n multiset<int> ms;\n vector<int> ans;\n for (int i=0;i<changed.size();++i){\n if (changed[i]==0) ++cnt;\n else ms.insert(changed[i]);\n }\n if (cnt%2==1) return {};\n else for (int i=0;i<cnt/2;++i) ans.push_back(0);\n while (ms.size()>0){\n if (ms.find(2*(*ms.begin()))!=ms.end()){\n ans.push_back(*ms.begin());\n ms.erase(ms.find(2*(*ms.begin())));\n ms.erase(ms.begin());\n }\n else return {};\n } \n return ans;\n }\n};",
"memory": "177573"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n=changed.size();\n multiset<int>mt;\n vector<int>ans;\n int cnt0=0;\n for(int i=0;i<n;i++)\n {\n if(changed[i]==0)\n {\n cnt0++;\n continue;\n }\n mt.insert(changed[i]);\n }\n if(cnt0%2)\n {\n return {};\n }\n else\n {\n for(int i=0;i<cnt0/2;i++)\n {\n ans.push_back(0);\n }\n }\n\n while(!mt.empty())\n {\n auto it=mt.end();\n it--;\n int val=*it;\n if(val%2)\n {\n return {};\n }\n if(mt.find(val/2)==mt.end())\n {\n return {};\n }\n else\n {\n ans.push_back(val/2);\n mt.erase(mt.find(val));\n mt.erase(mt.find(val/2));\n }\n }\n\n return ans;\n }\n};",
"memory": "178574"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size();\n if(n%2)return {};\n multiset<int>st;\n int zeroCnt = 0;\n for(int i=0;i<n;i++){\n if(changed[i]==0)\n zeroCnt++;\n else\n st.insert(changed[i]);\n }\n if(zeroCnt %2)return {};\n vector<int>res;\n sort(changed.begin(),changed.end());\n for(int i=0;i<n;i++){\n int curr = changed[i];\n int doubled = 2*changed[i];\n auto it = st.find(curr);\n auto it2 = st.find(doubled);\n \n if(curr==0){\n if(zeroCnt){\n res.push_back(0);\n zeroCnt-=2;\n }\n }\n if(it!=st.end() and it2!=st.end()){\n st.erase(it);\n st.erase(it2);\n res.push_back(curr);\n }\n }\n if(st.size())return {};\n return res;\n }\n};",
"memory": "179575"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n const int nmax = 1e5;\n int n = changed.size();\n\n if (n % 2) return vector<int>();\n\n vector<int> cnt(nmax + 1);\n for (int i: changed) cnt[i]++;\n if (cnt[0] % 2) return vector<int>();\n vector<int> res;\n for (int i = 1; i <= cnt[0] / 2; i++) res.push_back(0);\n for (int i = 1; i <= nmax; i++) {\n if (cnt[i]) {\n if (i * 2 > nmax || cnt[i] > cnt[i * 2]) return vector<int>();\n for (int j = 1; j <= cnt[i]; j++) res.push_back(i);\n cnt[i * 2] -= cnt[i];\n cnt[i] = 0;\n }\n }\n\n return res;\n }\n};",
"memory": "180576"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // if(changed=={1,2,1,0}) return {};\n unordered_map<int,vector<int>> mpp;\n int cnt=0;\n int n=changed.size();\n if(n&1) return {};\n sort(changed.begin(),changed.end());\n vector<int> visited(n,0);\n for(int i=0;i<n;i++){\n if(changed[i]==0) cnt++;\n else mpp[changed[i]].push_back(i);\n }\n vector<int> ans;\n if(cnt%2==0){\n for(int i=0;i<cnt/2;i++) ans.push_back(0);\n }\n for(int i=0;i<n;i++){\n int num=changed[i];\n if(num!=0){\n if(visited[i]==0){\n if(mpp.find(num*2)!=mpp.end()){\n\n int i=0;\n int flag=0;\n while(i<mpp[num*2].size()){\n if(visited[mpp[num*2][i]]==0){\n visited[mpp[num*2][i]]=1;\n ans.push_back(num);\n flag=1;\n break;\n }\n i++;\n }\n if(flag==0) return {};\n }\n else{\n return {};\n }\n visited[i]=1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "181578"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n vector<int> findOriginalArray(vector<int>& changed)\n {\n if (changed.size() % 2 != 0)\n return {};\n\n\n vector<int> nums(100001);\n for (size_t i = 0; i < changed.size(); i++)\n nums[changed[i]]++;\n\n if (nums[0] % 2 != 0)\n return {};\n\n vector<int> result(nums[0] / 2, 0);\n for (size_t i = 1; i <= nums.size() / 2; i++)\n {\n if (nums[i] == 0)\n continue;\n\n if (nums[i] <= nums[i * 2])\n {\n nums[i * 2] -= nums[i];\n result.insert(result.end(), nums[i], i);\n }\n else\n {\n return{};\n }\n }\n\n return result;\n }\n};",
"memory": "182579"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n vector<int> findOriginalArray(vector<int>& changed)\n {\n if (changed.size() % 2 != 0)\n return {};\n\n\n vector<int> nums(100001);\n for (size_t i = 0; i < changed.size(); i++)\n nums[changed[i]]++;\n\n if (nums[0] % 2 != 0)\n return {};\n\n vector<int> result(nums[0] / 2, 0);\n for (size_t i = 1; i <= nums.size() / 2; i++)\n {\n if (nums[i] == 0)\n continue;\n\n if (nums[i] <= nums[i * 2])\n {\n nums[i * 2] -= nums[i];\n result.insert(result.end(), nums[i], i);\n }\n else\n {\n return{};\n }\n }\n\n return result;\n }\n};",
"memory": "183580"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution1 {\n public:\n vector<int> findOriginalArray(vector<int>& changed) {\n sort(changed.begin(),changed.end());\n vector<int> original;\n int i,j;\n for(i=0;i<changed.size();i++){\n if(changed[i]==-1)continue;\n else{\n j=getPosition(changed[i]*2,changed);\n if(j==-1)return {};\n changed[j]=-1;\n original.push_back(changed[i]);\n }\n }\n return original;\n }\n\n\n int getPosition(int target,const vector<int> &arr){\n int pos;\n for(pos=0;pos<arr.size();pos++){\n if(target==arr[pos])return pos;\n }\n return -1;\n }\n};\n\n\n\nclass Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if(changed.size()%2)return {};\n\n vector<int> original;\n int i,j;\n map<int,vector<int>> positions;\n\n sort(changed.begin(),changed.end());\n\n for(i=0;i<changed.size();i++)\n positions[changed[i]].push_back(i);\n \n\n for(i=0;i<changed.size();i++){\n if(changed[i]==-1)continue;\n else{\n j=getPosition(changed[i]*2,changed,positions);\n if(j==-1)return {};\n changed[j]=-1;\n original.push_back(changed[i]);\n }\n }\n return original;\n }\n\n\n int getPosition(int target,const vector<int> &arr,map<int,vector<int>> &positions){\n\n if(positions.find(target)==positions.end() or positions[target].size()==0)return -1;\n \n int ans=positions[target][positions[target].size()-1];\n positions[target].pop_back();\n return ans; \n }\n};",
"memory": "184581"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n map<int, int> mp;\n vector<int> original;\n vector<int> empty;\n if (changed.size()%2 == 1)return empty;\n\n for (int i = 0; i < changed.size(); i++) {\n mp[changed[i]]++;\n }\n\n for (auto it : mp) {\n if (it.first < INT_MAX / 2) {\n if (mp[2 * it.first] < it.second)\n return empty;\n else {\n mp[2 * it.first] -= it.second;\n if (it.first == 0 )\n it.second /= 2;\n for (int i = 0; i < it.second; i++) {\n original.push_back(it.first);\n }\n }\n }\n }\n return original;\n }\n};",
"memory": "185583"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "static int io_opt = []() {\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nclass Solution {\n public:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() & 1) return vector<int>();\n sort(changed.begin(), changed.end());\n vector<int> cache(100001, 0);\n int count = 0;\n int i, t;\n int size = changed.size();\n int max_size = size / 2;\n\n // skip 0s.\n for (i = 0; i < size && !changed[i]; ++i)\n ;\n if (i & 1) return vector<int>();\n vector<int> ans(i / 2, 0);\n for (; i < size; ++i) {\n t = changed[i];\n if (t & 1) {\n ans.push_back(t);\n ++cache[t];\n } else {\n if (cache[t / 2] > 0) {\n --cache[t / 2];\n } else {\n ans.push_back(t);\n ++cache[t];\n }\n }\n if (ans.size() > max_size) return vector<int>();\n }\n\n if (ans.size() == max_size) return ans;\n\n return vector<int>();\n }\n};",
"memory": "186584"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // if(changed=={1,2,1,0}) return {};\n unordered_map<int,vector<int>> mpp;\n int cnt=0;\n int n=changed.size();\n if(n&1) return {};\n sort(changed.begin(),changed.end());\n vector<int> visited(n,0);\n for(int i=0;i<n;i++){\n if(changed[i]==0) cnt++;\n else mpp[changed[i]].push_back(i);\n }\n vector<int> ans;\n if(cnt%2==0){\n for(int i=0;i<cnt/2;i++) ans.push_back(0);\n }\n for(int i=0;i<n;i++){\n int num=changed[i];\n if(num!=0){\n if(visited[i]==0){\n if(mpp.find(num*2)!=mpp.end()){\n vector<int> temp=mpp[num*2];\n int i=0;\n int flag=0;\n while(i<temp.size()){\n if(visited[mpp[num*2][i]]==0){\n visited[mpp[num*2][i]]=1;\n ans.push_back(num);\n flag=1;\n break;\n }\n i++;\n }\n if(flag==0) return {};\n }\n else{\n return {};\n }\n visited[i]=1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "187585"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n // if(changed=={1,2,1,0}) return {};\n unordered_map<int,vector<int>> mpp;\n int cnt=0;\n int n=changed.size();\n if(n&1) return {};\n sort(changed.begin(),changed.end());\n vector<int> visited(n,0);\n for(int i=0;i<n;i++){\n if(changed[i]==0) cnt++;\n else mpp[changed[i]].push_back(i);\n }\n vector<int> ans;\n if(cnt%2==0){\n for(int i=0;i<cnt/2;i++) ans.push_back(0);\n }\n for(int i=0;i<n;i++){\n int num=changed[i];\n if(num!=0){\n if(visited[i]==0){\n if(mpp.find(num*2)!=mpp.end()){\n vector<int> temp=mpp[num*2];\n int i=0;\n int flag=0;\n while(i<temp.size()){\n if(visited[mpp[num*2][i]]==0){\n visited[mpp[num*2][i]]=1;\n ans.push_back(num);\n flag=1;\n break;\n }\n i++;\n }\n if(flag==0) return {};\n }\n else{\n return {};\n }\n visited[i]=1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "187585"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& nums) {\n int n=nums.size();\n if(n%2)return {};\n sort(nums.begin(),nums.end());\n unordered_map<int,int>mpp;\n\n for(auto it:nums){\n mpp[it]++;\n }\n //int cnt=0;\n vector<int>ans;\n\n for(auto it:nums){\n if(it==0&&mpp[it]>1){\n while(mpp[it]>1){\n ans.push_back(0);\n mpp[it]-=2;\n }\n }\n else if(it!=0&&mpp.find(it*2)!=mpp.end()&&mpp.find(it)!=mpp.end()){\n ans.push_back(it);\n mpp[it]--;\n mpp[it*2]--;\n }\n if(mpp[it]==0)mpp.erase(it);\n if(mpp[it*2]==0)mpp.erase(it*2);\n }\n\n if(ans.size()==n/2)return ans;\n return {};\n }\n};",
"memory": "188586"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if(changed.size() % 2) return {};\n sort(changed.rbegin(), changed.rend());\n vector<int> ans;\n int n = changed.size();\n unordered_multiset<int> s;\n for(auto num : changed) s.insert(num);\n for(int i = 0; i < n; i++) {\n auto k = s.find(changed[i]);\n if(k == s.end()) continue; \n s.erase(k);\n if(changed[i]%2) return {};\n k = s.find(changed[i]/2);\n if(k == s.end()) return {};\n ans.push_back(*k);\n s.erase(k);\n }\n return ans;\n }\n};",
"memory": "189588"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n int n = changed.size(), st =0;\n if(n % 2) return {};\n sort(changed.rbegin(), changed.rend());\n vector<int> ans(n/2);\n unordered_multiset<int> s;\n for(auto num : changed) s.insert(num);\n for(int i = 0; i < n; i++) {\n auto k = s.find(changed[i]);\n if(k == s.end()) continue; \n s.erase(k);\n if(changed[i]%2) return {};\n k = s.find(changed[i]/2);\n if(k == s.end()) return {};\n ans[st++] = *k;\n s.erase(k);\n }\n return ans;\n }\n};",
"memory": "190589"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> findOriginalArray(vector<int>& changed) {\n if (changed.size() % 2 != 0) return {};\n multiset<int> s ;\n vector<int> ans ;\n for(auto it: changed){\n s.insert(it);\n }\n\n while(! s.empty()){\n int small = *s.begin();\n s.erase(s.find(small));\n // cout << \" hi\" ;\n\n if(s.find(2*small) == s.end()) return {};\n\n else { ans.push_back(small);\n s.erase(s.find(2*small));}\n\n }\n return ans ;\n\n \n\n\n }\n};",
"memory": "191590"
} |
2,117 | <p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> changed = [1,3,4,2,6,8]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> changed = [6,3,0,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> changed = [1]
<strong>Output:</strong> []
<strong>Explanation:</strong> changed is not a doubled array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // vector<int> findOriginalArray(vector<int>& changed) {\n // multiset<int> ms;\n // for(int i=0;i<changed.size();i++) ms.insert(changed[i]);\n // vector<int> ans;\n // for(auto ele : ms){\n // if(ms.find(ele*2)!=ms.end()){\n // ans.push_back(ele);\n // ms.erase(ele);\n // ms.erase(ele*2);\n // }\n // else if(ms.find(ele/2)!=ms.end() && ele%2==0){\n // ans.push_back(ele/2);\n // ms.erase(ele/2);\n // ms.erase(ele);\n // }\n // }\n // if(ans.size()==changed.size()/2){\n // return ans;\n // }\n // return {};\n // }\n vector<int> findOriginalArray(vector<int>& changed) {\n multiset<int> ms(changed.begin(), changed.end());\n vector<int> ans;\n\n while (!ms.empty()) {\n int ele = *ms.begin();\n ms.erase(ms.begin());\n\n if (ms.find(ele * 2) != ms.end()) {\n ans.push_back(ele);\n ms.erase(ms.find(ele * 2));\n }\n else if (ele % 2 == 0 && ms.find(ele / 2) != ms.end()) {\n ans.push_back(ele / 2);\n ms.erase(ms.find(ele / 2));\n }\n else {\n return {};\n }\n }\n\n if (ans.size() == changed.size() / 2) {\n return ans;\n }\n\n return {};\n}\n\n};",
"memory": "192591"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n const int n=nums.size();\n int ans=n;\n \n ranges::sort(nums);\n nums.erase(unique(nums.begin(),nums.end()),nums.end());\n\n for(int i=0;i<n;++i){\n const int start=nums[i];\n const int end=start+n-1;\n const int index=firstGreater(nums,end);\n const int uniqueLength=index-i;\n ans = min(ans,n-uniqueLength);\n } \n return ans;\n }\nprivate:\n int firstGreater(const vector<int>& nums,int target){\n return ranges::upper_bound(nums,target)-nums.begin();\n } \n};",
"memory": "66700"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n const int n = nums.size();\n int ans=n;\n\n ranges::sort(nums);\n nums.erase(unique(nums.begin(),nums.end()),nums.end());\n\n for(int i=0;i<n;++i){\n const int start = nums[i];\n const int end = start+n-1;\n const int index = firstGreater(nums,end);\n const int uniqueLength = index - i;\n ans = min(ans,n-uniqueLength);\n } \n return ans;\n }\nprivate:\n int firstGreater(const vector<int>& nums,int target){\n return ranges::upper_bound(nums,target)-nums.begin();\n } \n};",
"memory": "66800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n int minOperations(vector<int>& nums) {\n const int n = nums.size();\n int ans = n;\n\n ranges::sort(nums);\n nums.erase(unique(nums.begin(), nums.end()), nums.end());\n\n for (int i = 0; i < nums.size(); ++i) {\n const int start = nums[i];\n const int end = start + n - 1;\n const int index = firstGreater(nums, end);\n const int uniqueLength = index - i;\n ans = min(ans, n - uniqueLength);\n }\n\n return ans;\n }\n\n private:\n int firstGreater(const vector<int>& A, int target) {\n return ranges::upper_bound(A, target) - A.begin();\n }\n};",
"memory": "66900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n int minOperations(vector<int>& nums) {\n const int n = nums.size();\n int ans = n;\n\n ranges::sort(nums);\n nums.erase(unique(nums.begin(), nums.end()), nums.end());\n\n for (int i = 0; i < nums.size(); ++i) {\n const int start = nums[i];\n const int end = start + n - 1;\n const int index = firstGreater(nums, end);\n const int uniqueLength = index - i;\n ans = min(ans, n - uniqueLength);\n }\n\n return ans;\n }\n\n private:\n int firstGreater(const vector<int>& A, int target) {\n return ranges::upper_bound(A, target) - A.begin();\n }\n};",
"memory": "67000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int m = unique(nums.begin(), nums.end()) - nums.begin();\n int n = nums.size();\n int ans = n;\n for (int i = 0; i < m; ++i) {\n int j = upper_bound(nums.begin() + i, nums.begin() + m, nums[i] + n - 1) - nums.begin();\n ans = min(ans, n - (j - i));\n }\n return ans;\n }\n};",
"memory": "67800"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // time/space: O(nlogn)/O(n)\n int minOperations(vector<int>& nums) {\n // get the sorted and unique integers\n int n = nums.size();\n sort(nums.begin(), nums.end());\n int k = unique(nums.begin(), nums.end()) - nums.begin();\n \n // slide the window within a valid range\n int result = INT_MAX;\n for (int l = 0, r = 0; r < k; r++) {\n while ((l < r) && ((nums[l] + n - 1) < nums[r])) l++;\n result = min(result, n - (r - l + 1));\n }\n return result;\n }\n};",
"memory": "67900"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n int n = nums.size();\n sort(nums.begin(), nums.end());\n int k = unique(nums.begin(), nums.end()) - nums.begin();\n //cout << k << \"\\n\";\n int result = INT_MAX;\n for (int l = 0, r = 0; r < k; r++) {\n while ((l < r) && ((nums[l] + n - 1) < nums[r])) l++;\n result = min(result, n - (r - l + 1));\n }\n return result;\n }\n};",
"memory": "68000"
} |
2,119 | <p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
<ul>
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already continuous.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // time/space: O(nlogn)/O(n)\n int minOperations(vector<int>& nums) {\n // get the sorted and unique integers\n int n = nums.size();\n sort(nums.begin(), nums.end());\n int k = unique(nums.begin(), nums.end()) - nums.begin();\n \n // slide the window within a valid range\n int result = INT_MAX;\n for (int l = 0, r = 0; r < k; r++) {\n while ((l < r) && ((nums[l] + n - 1) < nums[r])) l++;\n result = min(result, n - (r - l + 1));\n }\n return result;\n }\n};",
"memory": "68100"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.