<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>&nbsp;</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>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= changed.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= changed[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution1 {
    public:
    vector<int> findOriginalArray(vector<int>& changed) {
        sort(changed.begin(),changed.end());
        vector<int> original;
        int i,j;
        for(i=0;i<changed.size();i++){
            if(changed[i]==-1)continue;
            else{
                j=getPosition(changed[i]*2,changed);
                if(j==-1)return {};
                changed[j]=-1;
                original.push_back(changed[i]);
            }
        }
        return original;
    }


    int getPosition(int target,const vector<int> &arr){
        int pos;
        for(pos=0;pos<arr.size();pos++){
            if(target==arr[pos])return pos;
        }
        return -1;
    }
};



class Solution {
public:
    vector<int> findOriginalArray(vector<int>& changed) {
        if(changed.size()%2)return {};

        vector<int> original;
        int i,j;
        map<int,vector<int>> positions;

        sort(changed.begin(),changed.end());

        for(i=0;i<changed.size();i++)
            positions[changed[i]].push_back(i);
        

        for(i=0;i<changed.size();i++){
            if(changed[i]==-1)continue;
            else{
                j=getPosition(changed[i]*2,changed,positions);
                if(j==-1)return {};
                changed[j]=-1;
                original.push_back(changed[i]);
            }
        }
        return original;
    }


    int getPosition(int target,const vector<int> &arr,map<int,vector<int>> &positions){

        if(positions.find(target)==positions.end() or positions[target].size()==0)return -1;
 
        int ans=positions[target][positions[target].size()-1];
        positions[target].pop_back();
        return ans;      
    }
};
184581