<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 Solution
{
public:
    vector<int> findOriginalArray(vector<int> &changed)
    {
        int n = changed.size();
        sort(changed.rbegin(),changed.rend());
        if(n%2==0)
        {
        map<int, int> mpp;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            mpp[changed[i]]++;
        }
        for (int i=0;i<n;i++)
        {
            int f = changed[i];
            if (f % 2 == 0 && f!=0)
            {
                if (mpp.find(f) != mpp.end() && mpp.find(f / 2) != mpp.end())
                {
                    v.push_back(f / 2);
                    mpp[f]--;
                    if (mpp[f] == 0)
                    {
                        mpp.erase(f);
                    }
                    mpp[f / 2]--;
                    if (mpp[f / 2] == 0)
                    {
                        mpp.erase(f/2);
                    }
                }
                else if (mpp.find(f) != mpp.end() && mpp.find(2 * f) != mpp.end())
                {
                    v.push_back(f);
                    mpp[f]--;
                    if (mpp[f] == 0)
                    {
                        mpp.erase(f);
                    }
                    mpp[2 * f]--;
                    if (mpp[2 * f] == 0)
                    {
                        mpp.erase(2 * f);
                    }
                }
            }
            else if(f%2==0 && f==0)
            {
                if(mpp[f]>=2)
                {
                    v.push_back(f);
                    mpp[f]--;
                    mpp[f]--;
                    if(mpp[f]==0)
                    {
                        mpp.erase(f);
                    }
                }
            }
            else
            {

                if (mpp.find(f) != mpp.end() && mpp.find(2 * f) != mpp.end())
                {
                    v.push_back(f);
                    mpp[f]--;
                    if (mpp[f] == 0)
                    {
                        mpp.erase(f);
                    }
                    mpp[2 * f]--;
                    if (mpp[2 * f] == 0)
                    {
                        mpp.erase(2 * f);
                    }
                }
            }
        }
        if (v.size() == n / 2)
        return v;
        else
        return {};
        }
        else
        {
            return {};
        }
    }
};
152541