Relative Sort Array | Sort an array according to the other | Hashing | Leetcode DSA series | Hindi

Sdílet
Vložit
  • čas přidán 27. 05. 2021
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Relative Sort Array" from Leetcode which is solved by using a vector, map in c++.
    Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
    Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
    Sort the elements of arr1 such that the relative ordering of items in arr1 is the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
    Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
    Output:
    [2,2,2,1,4,3,3,9,6,7,19]
    We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
    ----------------------------------------------------------------------------------------
    Relative Sort Array: leetcode.com/problems/relativ...
    Sort an array according to the other: practice.geeksforgeeks.org/pr...
    Home Work ( Custom Sort String ): leetcode.com/problems/custom-...
    code in This Video: github.com/Prince-1501/Hello_...
    Pdf in this video: github.com/Prince-1501/Hello_...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    Telegram► telegram.me/helloworldbyprince
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ►Competitive Programming: • How to start Competiti...
    ►C++ Full Course : • L-01 || Introduction a...
    ►Algorithms: • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
    ✨ Tags ✨
    how to solve 1122. Relative Sort Array
    Sort an array according to the other
    How to solve 791. Custom Sort String
    question asked in Google
    how to crack Goldman Sachs online test
    how to crack Google Interview
    off-campus placement
    how to learn to code for beginners
    Practice Hashing data structure
    hashing in data structure
    Best Telegram channel for Off-campus Placement drive
    hashing in a data structure in Hindi
    unordered_map
    #hashing #geeksforgeeks #programming

Komentáře • 106

  • @deveshudhlani8295
    @deveshudhlani8295 Před 2 lety +10

    Literally you're doing the great work!
    And all those who are here and reading my comment, this series of hasing is going to help a lot in placements!!
    ✌🏻✌🏻

  • @user-ev5tm2sx6n
    @user-ev5tm2sx6n Před 14 dny

    By seeing the question I thought mujhse nhi hoga lekin phir aapne bola ki try kro to thodi sharm aa gyi and I triied aur ho gya. Thanks bro

  • @remedyiq8034
    @remedyiq8034 Před 2 lety +1

    BHUT MAST PADHTEY HO .... MERE KO HASHING MEI BHUT CONFIDENCE AH GYA HA. Following the golden rule of this channel ..phele khudse!!!.

  • @mdumarhabib5341
    @mdumarhabib5341 Před 8 dny

    Thank you Prince Bhaijaan

  • @salmaniproductions1104

    Solved by own. Thank you so much bhaiyya.

  • @pritishpattnaik4674
    @pritishpattnaik4674 Před 2 lety +6

    I tried on my own and reached almost 90% problem , but was not able to push the remaining elements of array 1 to ans vector in ascending order , great video Prince bhai

  • @eshaanmandal5150
    @eshaanmandal5150 Před 2 lety

    Custom Sorting Question was done easily after I understood this question!! Thanks for the video man !!

  • @Shubham-zu1zh
    @Shubham-zu1zh Před rokem +2

    biklul esa hi kiya tha , thanks for your efforts,HW done

  • @anonymous090
    @anonymous090 Před 2 lety

    I have done this before watching the video and submitted it with 0 ms. All thanks to you bhaiya💕

  • @masumali8356
    @masumali8356 Před rokem

    you are the best.......masum.

  • @akmarkan2490
    @akmarkan2490 Před 29 dny

    Thanks a lot

  • @arbazkhan5884
    @arbazkhan5884 Před rokem

    leetcode problem 791 is done and thanks for u encourage to first slove problem itself and then watch ur video and i'm not have that habits but now yes

  • @naman120
    @naman120 Před 2 lety +5

    class Solution {
    public:
    string customSortString(string order, string s) {
    unordered_map A;
    string s1;
    for(int i = 0 ; i< s.size();i++)
    {
    A[s[i]]++;
    }
    for(int i = 0 ; i< order.size();i++)
    {
    if(A.find(order[i])!=A.end())
    {
    for(int j = 0 ; j< A[order[i]];j++)
    {
    s1=s1+order[i] ;
    }
    A.erase(order[i]);
    }
    }
    for(auto x : A)
    {
    for(int i = 0 ; i

  • @vasujhawar.6987
    @vasujhawar.6987 Před rokem

    whyyyyyyy.........tell us inplace or better approach. We need to change the vector arr1 itself, not use extra vector. Why take extra space................

  • @binodkepapa4746
    @binodkepapa4746 Před 3 lety +3

    Bro doing great job 🙏🙏

  • @user-tt4pr4xh2o
    @user-tt4pr4xh2o Před dnem

    bhaiya what is the best way to solve dsa problems ... easy then meduim and then hard or mix ?

  • @Alokkumar-xu8sf
    @Alokkumar-xu8sf Před 3 lety +3

    maza aa gaya bhaiya

  • @udaytewary3809
    @udaytewary3809 Před rokem +1

    Homework question solve ho gya bhaiya

  • @avibirla9863
    @avibirla9863 Před 2 lety

    Bhaiya bohot achha video tha ye ....maza aa gaya ..Ly ♥️

  • @rupal6181
    @rupal6181 Před 2 lety

    Sir , I have done your homework . Thank You

  • @ayushikotiyal8466
    @ayushikotiyal8466 Před 2 lety

    i have a doubt . in a map all elements stored are in ascending order then in first loop we will get 1 first hence in output overall arr 2 elements will also be in ascending order since its a map. so how is ans correct?

  • @amanahmed6057
    @amanahmed6057 Před 2 lety +1

    4:33
    YE BADI ACHI BAAT KAHI AAPNE (Paresh raaval)

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      Thanks ☺️☺️
      Kabhi kabhi kuch jyada hi dialogue de dete hai

  • @cr7johnChan
    @cr7johnChan Před 2 lety +1

    Tried it myself,as soon as it clicked ,I was over the moon.

  • @Kumarb694
    @Kumarb694 Před rokem

    thank you bhai

  • @pritamsarkar3371
    @pritamsarkar3371 Před 3 lety +2

    great bro

  • @samiulislamdurjoy
    @samiulislamdurjoy Před 2 lety +1

    5:02

  • @abhaygupta4242
    @abhaygupta4242 Před rokem

    Bhaiya your lecture is very very good...

  • @oqant0424
    @oqant0424 Před rokem

    15/18 done (11.12.22)

  • @pritishpattnaik4674
    @pritishpattnaik4674 Před 2 lety +3

    My approach
    vector relativeSortArray(vector& arr1, vector& arr2) {
    map mp;
    int n = arr1.size();
    int m = arr2.size();
    vector ans;

    for (int i=0 ; isecond;
    while (v != 0){
    ans.push_back(x);
    v--;
    }
    }
    return ans;
    }

  • @rode_atharva
    @rode_atharva Před 27 dny

    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    map mp;
    vector ans;
    vectortemp;
    int m = arr1.size();
    int n = arr2.size();
    for(int i=0; i first);
    }
    }
    }
    return ans;
    }
    };

  • @beinghappy9223
    @beinghappy9223 Před 2 lety +2

    vector relativeSortArray(vector& arr1, vector& arr2) {
    sort(arr1.begin(),arr1.end());
    mapumap;
    for(int i=0;i

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +2

      Amazing buddy ❤️

    • @luckilygamer5462
      @luckilygamer5462 Před 2 lety

      code is not working in GFG because of sorting

    • @luckilygamer5462
      @luckilygamer5462 Před 2 lety

      *******************GFG Code***************************
      vector sortA1ByA2(vector A1, int N, vector A2, int M)
      {unordered_mapumap;
      vectorans;
      for(int i=0;i

  • @SubhamKumar-9009
    @SubhamKumar-9009 Před 3 lety

    awesome

  • @MuhammadHaddi-bs5nh
    @MuhammadHaddi-bs5nh Před 10 měsíci

    I done it

  • @wealthzeus
    @wealthzeus Před rokem

    I solved it using unordered map

  • @user-ll5qj9tj5c
    @user-ll5qj9tj5c Před 28 dny

    sir constraints dekhiye ..isko hm counting sort se bhi kar skte hai

  • @abhijitroy1958
    @abhijitroy1958 Před 2 lety +1

    //logic pehele hi a gaya tha but map looping ke indentation me thora problem a raha tha , but apne itne bar bola ki khud se try karo khud se solve karo , khud se dhoka matt karo, to mai ne bhi than liya ab to khud se hi karna hai and boom , first pass me ho gaya .sukriya dat neke liye
    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    mapmpp;
    for(auto it:arr1){
    mpp[it]++;
    }
    vectorv;
    for(int i=0;i

  • @rajchinagundi7498
    @rajchinagundi7498 Před rokem

    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    vector ans;
    vector temp;
    unordered_map mp;
    for(int i=0;i

  • @ankur4140
    @ankur4140 Před 2 lety

    Hello bhaiya , i first found the size of the array and then applied the loop
    int arr1_size, arr2_size;
    arr1_size = sizeof(arr1)/sizeof(arr1[0]);
    arr2_size = sizeof(arr2)/sizeof(arr2[0]);
    for(int i =0;i

  • @MuhammadHaddi-bs5nh
    @MuhammadHaddi-bs5nh Před 10 měsíci

    done all

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 10 měsíci +1

      nice Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @PROTECHRAHUL
    @PROTECHRAHUL Před 2 lety +1

    Bhaiya mujhe khud se question samj nii aata h but CZcams oe question samjne k baad me khud se jyadatar questions solve kar leta hu toh koi problem toh nhi h na

  • @remedyiq8034
    @remedyiq8034 Před 2 lety +1

    LEET CODE SOLUTION FOR 100 % fast result
    vector temp;
    map hash;
    for(int i=0;i

  • @Atpugtihsrah
    @Atpugtihsrah Před 3 lety +1

    Thanks for creating the video.
    Easy to understand Solution:
    ---------------------------------------------
    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    map map;
    vector res;

    // Get the frequency of each letter of arr1 in a map
    for(auto a: arr1)
    map[a]++;

    // For all the elements in arr2, get the actual freq
    // and add it in the result, remove them from the map
    for(auto a: arr2){
    for(int i=0; i

  • @masumali8356
    @masumali8356 Před rokem

    class Solution {
    public:
    string customSortString(string order, string s) {
    unordered_mapmp;
    string ans;
    for(auto i:s)
    {
    mp[i]++;
    }
    for(auto i:order)
    {
    if(mp.find(i)!=mp.end())
    {
    auto x=mp.find(i);
    int count=x->second;
    char found=x->first;
    while(count>0)
    {
    ans=ans+found;
    count--;
    }
    mp.erase(i);
    }
    }
    for(auto i:mp)
    {
    int count=i.second;
    char found=i.first;
    while(count>0)
    {
    ans=ans+found;
    count--;
    }

    }
    return ans;
    }
    };

  • @eshaanmandal5150
    @eshaanmandal5150 Před 2 lety

    In Java use the TreeMap class and implement SortedMap interface
    class Solution{
    public static int[] sortA1ByA2(int A1[], int N, int A2[], int M)
    {
    SortedMap map = new TreeMap();
    for(int i : A1){
    map.put(i,map.getOrDefault(i,0)+1);
    }
    int index = 0; //start from the first index

    for(int j : A2){
    if(map.containsKey(j)){
    int count = map.get(j);
    while(count-- > 0){
    A1[index++] = j;
    }
    map.remove(j);
    }
    }
    for(int j:map.keySet()){
    int count = (Integer)map.get(j);
    while(count-- > 0){
    A1[index++] = j;
    }
    }

    return A1;
    }
    }

  • @surgeonofdeath8199
    @surgeonofdeath8199 Před 2 lety +1

    HOMEWORK: it was literally the same question LOL
    class Solution {
    public:
    string customSortString(string order, string s) {
    string ans;
    //step 1
    map mpp;
    for(auto it: s)
    mpp[it]++;
    //step 2
    for(int i=0;i0){
    ans.push_back(order[i]);
    count--;}
    }
    mpp.erase(order[i]);
    }
    //step 3
    for(auto it: mpp){
    int count=it.second;
    while(count>0){
    ans.push_back(it.first);
    count--;}
    }
    return ans;
    }
    };

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      yhi to kah rahe the sab same hota hai basic samjhne ke baad
      Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @surjeetsingh433
    @surjeetsingh433 Před 2 lety

    Yarr isme aapne jo second example liya h usme aur question me contradiction lag raha h kyoki question ne bola h ki arr2 ke sare elements arr1 me honge.

  • @sambhavsharma2780
    @sambhavsharma2780 Před 2 lety

    Home Work Solution :-
    class Solution {
    public:
    string customSortString(string order, string s) {
    // map the s string
    // traverse order string if order char is present in map then push into ans
    // now traverse s and push remain char into ans
    map mp;
    string ans = "";
    for(int i=0; i

  • @evelynsummer4020
    @evelynsummer4020 Před rokem

    Can you make video in english please really appreciate it

  • @anonymous090
    @anonymous090 Před 2 lety

    Enjoy the journey, while chasing the destination??

  • @oqant0424
    @oqant0424 Před rokem

    HOMEWORK QUESTION DONE
    class Solution {
    public:
    string customSortString(string order, string s) {
    mapmp;
    string ans="";
    //traverse in s
    for(auto x:s){
    mp[x]++;
    }
    //traverse in order
    for(auto y:order){
    if(mp.find(y)!=mp.end()){
    auto it=mp.find(y);
    int freq=it->second;
    char ele=it->first;
    string curr(freq,ele);
    ans.insert(ans.end(),curr.begin(),curr.end());
    mp.erase(y);
    }
    }
    //traverse in mp for rest elements;
    for(auto it:mp){
    int freq=it.second;
    char ele=it.first;
    string curr(freq,ele);
    ans.insert(ans.end(),curr.begin(),curr.end());
    }
    return ans;
    }
    };

  • @srijanshahi8587
    @srijanshahi8587 Před rokem

    home work prob done sir

  • @algoman6589
    @algoman6589 Před rokem

    TC: O(N)
    SC: O(1)(constant)
    public static int[] sortA1ByA2(int A1[], int N, int A2[], int M)
    {
    int []freq=new int[1000001];
    for(int i: A1){
    freq[i]++;
    }
    int ans[]=new int[N];
    int k=0;
    for(int i: A2){
    while(freq[i]-->0){
    ans[k++]=i;
    }
    }
    for(int i=1;i0){
    ans[k++]=i;
    }
    }
    return ans;
    }

  • @mdumarhabib5341
    @mdumarhabib5341 Před 8 dny

    Python solution(1122. Relative Sort Array)
    from collections import defaultdict
    d = defaultdict(int)
    for i in arr1:
    d[i] += 1
    ans = []
    for i in arr2:
    if i in d:
    ans.extend( [i] * d[i] )
    del d[i]
    for k,v in sorted(d.items()):
    ans.extend( [k] * v )
    return ans

  • @Sonukumar-um3cs
    @Sonukumar-um3cs Před 3 lety +5

    Tree ke sawal kb krwaoge aap??

  • @vinayvastrakar2788
    @vinayvastrakar2788 Před rokem +1

    banda tabadtod hay

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      thanks Vinay keep learning and keep sharing my channel with your friends and in colleges

  • @sambhavsharma2780
    @sambhavsharma2780 Před 2 lety

    Relative Sort Array:-
    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    map mp;
    vector ans;
    // map arr1 array with their count value
    for(int i=0; isecond;
    while(value>0){
    ans.push_back(key);
    value--;
    }
    }
    return ans;
    }
    };

  • @surgeonofdeath8199
    @surgeonofdeath8199 Před 2 lety +1

    Bina dekhe code likh diya vmro.....
    class Solution {
    public:
    vector relativeSortArray(vector& arr1, vector& arr2) {
    map mpp;
    for(auto i:arr1)
    mpp[i]++;
    vector ans;
    for(int i=0;i0){
    ans.push_back(arr2[i]);
    count--;}
    mpp.erase(arr2[i]);
    }
    for(auto it:mpp){
    int count=it.second;
    while(count>0){
    ans.push_back(it.first);
    count--;}
    }
    return ans;
    }
    };

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      waooo yaar
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @sarthakjoshi21
    @sarthakjoshi21 Před rokem

    bro pls explain in simple manner many time in this vedio u try to explain what we are doing in this line and then u forget to explain that line and u jump to next line for explaining

  • @tilakrajchoubey5534
    @tilakrajchoubey5534 Před 2 lety +1

    Koi bta skta hai kya ki java me kaise hoga?