jayati

Sort an array according to the other

Sep 17th, 2023
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. class Solution{
  2.     public:
  3.     vector<int> sortA1ByA2(vector<int> A1, int N, vector<int> A2, int M)
  4.     {
  5.         //Your code here
  6.         vector<int> ans;
  7.         map<int,int> m;
  8.         for(int i=0;i<N;i++)
  9.         {
  10.             m[A1[i]]++;
  11.         }
  12.         for(int i=0;i<M;i++)
  13.         {
  14.             if(m.find(A2[i])!=m.end())
  15.             {
  16.                 auto it =m.find(A2[i]);
  17.                 int c=it->second;
  18.                 vector<int> v(c,A2[i]);
  19.                 ans.insert(ans.end(),v.begin(),v.end());
  20.                 m.erase(A2[i]);
  21.             }
  22.         }
  23.         for(auto x : m){
  24.             auto ele = x.first;
  25.             int count = x.second;
  26.             vector<int> v(count, ele);
  27.             ans.insert(ans.end(), v.begin(), v.end());
  28.         }
  29.         return ans;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment