<p>Given two arrays <code>arr1</code> and <code>arr2</code>, the elements of <code>arr2</code> are distinct, and all elements in <code>arr2</code> are also in <code>arr1</code>.</p>

<p>Sort the elements of <code>arr1</code> such that the relative ordering of items in <code>arr1</code> are the same as in <code>arr2</code>. Elements that do not appear in <code>arr2</code> should be placed at the end of <code>arr1</code> in <strong>ascending</strong> order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
<strong>Output:</strong> [2,2,2,1,4,3,3,9,6,7,19]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
<strong>Output:</strong> [22,28,8,6,17,44]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr1.length, arr2.length &lt;= 1000</code></li>
	<li><code>0 &lt;= arr1[i], arr2[i] &lt;= 1000</code></li>
	<li>All the elements of <code>arr2</code> are <strong>distinct</strong>.</li>
	<li>Each&nbsp;<code>arr2[i]</code> is in <code>arr1</code>.</li>
</ul>

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr, vector<int>& brr) {
        int n=arr.size();
        int m=brr.size();
        map<int,int> mpp;
    for (int i = 0; i < n; i++) {
      mpp[arr[i]]++;
    }
    vector<int> ans;
    for(int i=0;i<brr.size();i++)
    {
        if(mpp.find(brr[i])!=mpp.end())
        {
            while(mpp[brr[i]]>0)
            {
                ans.push_back(brr[i]);
                mpp[brr[i]]--;
            }
            mpp.erase(brr[i]);
        }
    }

    vector<int> remaining;
    for(auto it:mpp)
    {
        while(it.second)
        {
            remaining.push_back(it.first);
            it.second--;
        }
    }
    sort(remaining.begin(),remaining.end());
    for(int num:remaining)
    {
        ans.push_back(num);
    }

    return ans;
    }
};
10800