Sort Array by Increasing Frequency | Lambda Expression | Hashing | Leetcode DSA series | Hindi

Sdílet
Vložit
  • čas přidán 10. 06. 2021
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Sort Array by Increasing Frequency" from Leetcode which is solved by using a unordered_map in c++.
    Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
    Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
    Return the sorted array.
    Input: nums = [1,1,2,2,2,3]
    Output: [3,1,1,2,2,2]
    Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
    ----------------------------------------------------------------------------------------
    Lambda Expression: www.geeksforgeeks.org/lambda-...
    Sort Array by Increasing Frequency: leetcode.com/problems/sort-ar...
    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 Sort Array by Increasing Frequency
    question asked in Google
    what is a lambda expression
    how to crack online coding 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 • 47

  • @oqant0424
    @oqant0424 Před rokem +4

    solved on my own without even looking at the solution.........
    thanks bhaiya from the bottom of my heart........all credit goes to u
    your series literally boosted my confidence

  • @vishaljain4642
    @vishaljain4642 Před 3 lety +5

    Gazab Explanation 🔥🔥🔥

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

    also, you can try hashmap and minheap, which gives same ans with the same complexity

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

    teen account se subscibe kiya hu,
    because you really deserve it ..................

  • @salmaniproductions1104

    Very well explained. Thank you bhaiyya

  • @masumali8356
    @masumali8356 Před rokem

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

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

    ❤️❤️❤️❤️❤️

  • @khushipandey6172
    @khushipandey6172 Před rokem +1

    Similar problem on gfg gives error of time limit exceeded after passing 119 test case out of 240 .
    Problem :Sorting element of an array by frequency on gfg
    Can you provide other more optimized solution?

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

    Sir bahut dino bad video aaya h
    Tree ke videos thoda jaldi jaldi dalna sir

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

    WHEN THE DAYS ARE SORROW AND NIGHT ARE BITTER,
    THEN A JOURNEY SEEMS LIKE A BURDEN,
    DONT LOSS YOUR HOPE , AND KEEP SOME SOAP AS A TOOL TO CLEAN YOUR PATH OF HURDEL.

  • @gauravparasar4571
    @gauravparasar4571 Před 4 dny

    costum comparator and lambda fn alg h ?

  • @shashwatdev2371
    @shashwatdev2371 Před 2 lety

    By using 2 ordered maps
    class Solution {
    public:
    vector frequencySort(vector& nums)
    {
    mapumap;
    map map_freq;
    vector final_vec;
    for(auto a:nums)
    {
    umap[a]++;//Saves numbers and their frequency
    }
    for(auto k:umap)
    {
    map_freq[k.second].push_back(k.first);// map like frequency->(vector containing numbers
    //having that frequency)
    }
    for(auto j:map_freq)
    {
    for(int i=j.second.size()-1;i>=0;i--)//reversing vector so that it can be sorted in descending order when frequency is same
    {
    for(int l=0;l

  • @lex-zt6uc
    @lex-zt6uc Před rokem

    thx bhaiya

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

    Iski time complexity O (log n) kaise hui?

  • @rode_atharva
    @rode_atharva Před měsícem

    /*Example Comparisons During Sorting
    Initial Vector:
    nums = {4, 6, 2, 6, 4, 4, 3}
    Comparator Function:
    sort(nums.begin(), nums.end(), [&](int a, int b){
    return umap[a] != umap[b] ? umap[a] < umap[b] : a > b;
    });
    Comparing 4 and 6:
    umap[4] = 3
    umap[6] = 2
    Since umap[4] (3) is not equal to umap[6] (2), compare based on frequency:
    umap[4] < umap[6] evaluates to 3 < 2 which is false.
    So, 4 should come after 6.
    Second Comparison
    Comparing 6 and 2:
    umap[6] = 2
    umap[2] = 1
    Since umap[6] (2) is not equal to umap[2] (1), compare based on frequency:
    umap[6] < umap[2] evaluates to 2 < 1 which is false.
    So, 6 should come after 2.
    Third Comparison
    Comparing 2 and 6:
    umap[2] = 1
    umap[6] = 2
    Since umap[2] (1) is not equal to umap[6] (2), compare based on frequency:
    umap[2] < umap[6] evaluates to 1 < 2 which is true.
    So, 2 should come before 6.
    Fourth Comparison
    Comparing 6 and 4:
    umap[6] = 2
    umap[4] = 3
    Since umap[6] (2) is not equal to umap[4] (3), compare based on frequency:
    umap[6] < umap[4] evaluates to 2 < 3 which is true.
    So, 6 should come before 4.
    Fifth Comparison
    Comparing 4 and 4:
    umap[4] = 3
    umap[4] = 3
    Since umap[4] is equal to umap[4], compare based on value:
    4 > 4 evaluates to false.
    So, the order remains unchanged.
    Final Sorted Vector
    After all necessary comparisons(SORT FUNCTION WILL SEE THIS, YOU MY FRIEND DONT CARE OF IT) and swaps, the final sorted vector nums is:
    {2, 3, 6, 6, 4, 4, 4}
    Summary of a and b in Comparisons
    During sorting, a and b are elements from the nums vector that are being compared.
    The custom comparator checks:
    If the frequencies of a and b are different (umap[a] != umap[b]), it returns true if a's frequency is less than b's frequency.
    If the frequencies are the same, it returns true if a is greater than b to ensure that larger elements come first for the same frequency.
    */

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

    In place of lamda function can we use comparator

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

    Bhaiya graph ki series kab ane wali he??

  • @samiulislamdurjoy
    @samiulislamdurjoy Před 2 lety

    7:22

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

    bhaiya apne bass code kr diya hai...vhi to samjna hai vhi nhi btaye aap

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      I will request u to please watch hashing playlist from starting buddy

  • @vitaminprotein2217
    @vitaminprotein2217 Před rokem +1

    heap sol
    unordered_mapumap;
    for(auto x:nums)umap[x]++;
    priority_queuemaxh;
    for(auto x:umap){
    maxh.push({-x.second,x.first});//- lga ke daal denge usse lest freq wla max bn jyega aur pop krne ke time seedha result aa jyega
    }
    vectortemp;
    while(!maxh.empty()){
    //to print acc to freq
    int freq = maxh.top().first;
    int element = maxh.top().second;
    for(int i=1;i

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

    Jun 19 2022, results are out of 6th sem yesterday. Placements are coming. Ask me after 1 year how am i and where am i

  • @oqant0424
    @oqant0424 Před rokem

    17/18 done (11.12.22)

  • @satyamsrivastava1871
    @satyamsrivastava1871 Před 2 lety

    bhaiya maine asa socha pr problem aa rha
    class Solution {
    public:
    vector frequencySort(vector& nums) {
    vector ans;
    unordered_map um;

    for(auto x:nums)
    {
    um[x]++;
    }
    set s;
    for(auto x:um)
    {
    auto temp=x.second;
    s[temp]++;
    }
    for(auto x:s)
    {
    ans.push_back(x);
    }
    return ans;
    }
    };

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      kya problem aa rha dry run karo koi ek example lekar samjh me aa jayega ki kya gaklti ho rha hai

  • @praveenanand5689
    @praveenanand5689 Před 2 lety

    how to solve this problem without using lambda function

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

    Yaha ye kaise determine hua ki a is greater than b hi hai???

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

      Maine kaha tha, lambda function me ye maan ke chal rahe hai ki a pahle aayega b se

  • @VaibhavSutar-xm3cn
    @VaibhavSutar-xm3cn Před 4 dny

    var frequencySort = function(nums) {
    let map = new Map()
    for(let i=0;ia[1]-b[1])
    // console.log(sortedMap)
    let index = 0
    for(let i=0;i0){
    sortedMap[i][1] = sortedMap[i][1] -1
    nums[index++] = sortedMap[i][0]
    }
    }
    return nums
    };
    solved by own now will watch your tutorial

  • @hrithikraj6587
    @hrithikraj6587 Před rokem

    Also in java??

  • @Kaifkhan-xs4lm
    @Kaifkhan-xs4lm Před rokem

    Bhaiya return wali line ek bar bta do smjh ni ayi Please Please Pleaseeeeeeeeeeeeeeeee.........

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      kya nhi samjha ussme aap ye batao ek baar
      please Hashing starting se dekho aap please
      and ek baar dry run v karo mere code ko
      its your homework