Minimum Number of Swaps Required to Sort the Array | Love Babbar DSA Sheet | GFG | FAANG🔥| Placement

Поділитися
Вставка
  • Опубліковано 9 лют 2025
  • #sorting and #searching #competitiveprogramming #coding #dsa
    Hey Guys in this video I have explained with code how we can solve the problem 'Minimum Number of Swaps Required to Sort the Array'.
    Array question Playlist = • Love Babbar DSA 450 Qu...
    String question Playlist = • Love Babbar DSA 450 Qu...
    Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
    Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
    Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
    Roadmap for Dynamic Programming = • Complete Roadmap for D...
    Great Strategy to solve DSA = • Great Strategy to solv...
    My Journey to 5 star at codechef = • My Journey to 5 Star a...
    Love Babbar DSA Sheet : drive.google.c...
    Follow us on Instagram:
    Subham Singh : / shubham__ig
    Shailesh Yogendra : / shaileshyogendra
    Yogesh Yogendra : / i_am_yogesh_here
    Follow us on LinkedIn:
    Subham Singh : / subham-singh-cc
    Shailesh Yogendra : / shailesh-yogendra-8b13...
    Yogesh Yogendra : / yogesh-yogendra-26bbb518a
    Hope you like it. Comment if you have any doubt
    LIKE | SHARE | SUBSCRIBE

КОМЕНТАРІ •

  • @adityapandey8245
    @adityapandey8245 3 роки тому +28

    Shukriya brother, you're way younger than me, but have so much more knowledge. Nice to learn from you bro

    • @damienarlo7196
      @damienarlo7196 3 роки тому

      I know im asking randomly but does someone know a method to log back into an Instagram account??
      I stupidly lost the password. I love any tricks you can offer me

  • @rakeshkumaryadav1117
    @rakeshkumaryadav1117 2 роки тому +1

    void swap(pair& a,pair& b)
    {
    pair temp=a;
    a=b;
    b=temp;
    }
    //Function to find the minimum number of swaps required to sort the array.
    int minSwaps(vector&nums)
    {
    // Code here
    int n=nums.size();
    vector v(n);
    for(int i=0;i

    • @vamsigaming9334
      @vamsigaming9334 2 роки тому

      You are using push_back function don't use that use v[I]={nums(I) , I} it works

    • @raunakkumar6144
      @raunakkumar6144 2 роки тому +1

      bro you cant do both declaring size and then using push back, either use index based or simply remove size from declaration

  • @Productreviews7583
    @Productreviews7583 4 роки тому +5

    Best explanation 👍

  • @satyaprasadjena4293
    @satyaprasadjena4293 4 роки тому +3

    best explanation. keep it up bro

  • @sumitprakash4650
    @sumitprakash4650 Рік тому

    Best explanation

  • @AmanVerma-zb1xf
    @AmanVerma-zb1xf 2 роки тому

    Bhai hindi wali feeling hi alag hoti hai 👍👍👍

  • @codetochange8
    @codetochange8 2 роки тому

    Thanks for very easy explanation....

  • @komalkrishna7836
    @komalkrishna7836 Рік тому

    Nice Approach!!

  • @codewithree930
    @codewithree930 4 роки тому +5

    What a approach!👏

  • @VikasKumar-mg9ck
    @VikasKumar-mg9ck 2 роки тому +1

    this was a great intution

  • @mohammedwaseem8599
    @mohammedwaseem8599 3 роки тому +1

    Op explanation .

  • @kanwarkajla
    @kanwarkajla 3 роки тому

    bahut mast bhai

  • @halchal41
    @halchal41 2 роки тому +1

    Bhaiya mera interview h kuch dino me microsoft ka..Maine aapke channel se tyari ki h,agar selection ho jata h to me aapko thanks donation dungi

  • @shovanchatterjee4350
    @shovanchatterjee4350 3 роки тому +4

    Describe it in graph approach if possible.

  • @utkarshsharma5202
    @utkarshsharma5202 Рік тому

    Will this approach work when duplicate elements are there?

  • @RavinderKumar-bn4ch
    @RavinderKumar-bn4ch 3 роки тому +12

    Bhai please sort ko short mt bola kr....

  • @Viralmems_xyz
    @Viralmems_xyz Рік тому

    nice explanation bro

  • @kollivenkatamadhukar5435
    @kollivenkatamadhukar5435 2 роки тому +1

    How is it guranteed that you will be doing min no swaps this way

  • @satyampaul7550
    @satyampaul7550 3 роки тому +6

    but how can this solution assure that this solution is giving min sawps?????

    • @illusionEditorss
      @illusionEditorss 3 роки тому

      Exactly

    • @codetogether24x7
      @codetogether24x7 3 роки тому +2

      Bro the idea behind this algorithm is to place the element exactly at its correct index..

  • @saranshgupta6769
    @saranshgupta6769 2 роки тому

    🙌

  • @melodytunes2204
    @melodytunes2204 3 роки тому +1

    bhai esa ni kar sakte ki ek alag array m saare elements store kara len aur fir usko sort karden
    aur uske baad loop chala kar jitne elements match nhi honge original array se uske half no of swaps honge

  • @prithviiiiiiiiiiiii5338
    @prithviiiiiiiiiiiii5338 3 роки тому

    nice explanation thanks .

  • @suryanshsingh6906
    @suryanshsingh6906 4 роки тому +1

    Great work

  • @divasbhadani9225
    @divasbhadani9225 4 роки тому +5

    Bhai mast explanation h ..
    Code sabhi jagah mil jayega but the good explanation isn't.

  • @AmazingWorld-fw9oc
    @AmazingWorld-fw9oc 3 роки тому +1

    all good but 18 min explanation was not needed at all.

  • @tishajain397
    @tishajain397 Рік тому

    Space complexity kitna hai isss code ka

  • @manideep1782
    @manideep1782 4 роки тому +12

    Good explanation 👍, but what if given array contains duplicates??

    • @saubhagyamarwaha8518
      @saubhagyamarwaha8518 3 роки тому

      Use some stable sort technique

    • @tempcoast_223
      @tempcoast_223 2 роки тому +1

      but i think in question it is already stated that "strickly increasing order" so that kind of input wont come in this question.
      but if you want to do for the duplicate then you should use stable sort.

  • @ABHIRAMR1
    @ABHIRAMR1 3 роки тому

    Legend 🔥🔥🔥

  • @dikshasingh1425
    @dikshasingh1425 3 роки тому

    Nice explanation

  • @McBc-g4o
    @McBc-g4o 3 місяці тому

    corner cases is not covered,

  • @satyanandprasad3029
    @satyanandprasad3029 3 роки тому

    op explanation!!!

  • @mohitmotwani9256
    @mohitmotwani9256 3 роки тому

    splendid!!!

  • @siddharthagarwal3927
    @siddharthagarwal3927 3 роки тому

    Bahiya aapne median of two sorted array ka solution nahi bataya
    Vo bhi bata dijiye

  • @sarangr8624
    @sarangr8624 4 роки тому

    Best 🙏🙏🙏

  • @srinivaskonduri7496
    @srinivaskonduri7496 3 роки тому

    AWESOME VIDEO

  • @manmeetsinghchhabra1027
    @manmeetsinghchhabra1027 3 роки тому

    can u explain why i--?

  • @amijugjug
    @amijugjug 3 роки тому

    Hats off dude

  • @shreyakjain9692
    @shreyakjain9692 3 роки тому

    thnx so much

  • @Piyush-yd1ez
    @Piyush-yd1ez 3 роки тому +1

    will it work on duplicate element?

  • @schrodingerscat6189
    @schrodingerscat6189 3 роки тому +5

    why do half of the people call sorting as "shorting" ?? 😆

  • @aasthajain92
    @aasthajain92 Рік тому

    Time limit error

  • @vaibhavchhabra800
    @vaibhavchhabra800 2 роки тому

    sort an array not short an array (Pronuncation)

  • @yashwantkumar4998
    @yashwantkumar4998 4 роки тому +6

    Good explanation but the way you talk is irritating !

    • @GdLongerHandle
      @GdLongerHandle 3 роки тому +7

      Learning from a younger person is what makes you irritating , nothing bad about his accent.

    • @yashwantkumar4998
      @yashwantkumar4998 3 роки тому

      @@GdLongerHandle I'm same as his age😂

    • @siddharthsharma8530
      @siddharthsharma8530 3 роки тому

      Just because you don't like the accent, doesn't make the content bad

    • @AnkitSingh-wq2rk
      @AnkitSingh-wq2rk 3 роки тому

      bhai tumhe solution ka explaination chahiye free mein aur phir bhi ungli karni hai gazab ho yar

    • @yashwantkumar4998
      @yashwantkumar4998 3 роки тому

      @@AnkitSingh-wq2rk es trh ki ungaliyan apko mauka deti hai khud ko improve krne ka. Rather than being quiet and see him grow slowly, tell him his weaknesses to let him grow fast

  • @VishalKumar-pk9ek
    @VishalKumar-pk9ek 4 роки тому

    bhaiya , parameters ka jhol ho jaataa hai kabhi kabhi
    aapne arr[] aur N size ke basis pe sol diya hai but gfg par vector leke &nums diya hai bas😢😢

    • @tulika2863
      @tulika2863 3 роки тому +1

      To find n , use n= mums.size() . Hope this helps :)

  • @ashutoshgupta6488
    @ashutoshgupta6488 4 роки тому

    Bhaiya beginner ke liye btaye please

  • @philipowino-jofon
    @philipowino-jofon 3 роки тому

    This is youtube man just speak english. I wish I could subscribe

  • @nitinkumar5974
    @nitinkumar5974 3 роки тому

    your accent is execellent 👍

  • @_gunjan_agrawal
    @_gunjan_agrawal Рік тому +2

    bhot ganda pdhate ho yr

  • @paulbarsha1740
    @paulbarsha1740 2 роки тому

    why this code isn't giving desired o/p?
    int minSwaps(vector&nums)
    {
    /*vectorans=nums;
    int cnt=0;
    sort(ans.begin(), ans.end());
    for(int i=0; i

  • @gopinath7846
    @gopinath7846 3 роки тому

    make this video on java pls

  • @SaadKarim-r6z
    @SaadKarim-r6z 10 місяців тому

    bhai sorted hota hai shorted nhi ,bolna sikh pehle

  • @sparks8754
    @sparks8754 3 роки тому

    bro TLE aara h isse

  • @swaroopkv4540
    @swaroopkv4540 2 роки тому

    Python code for this

  • @VaishnaviNigam
    @VaishnaviNigam 3 роки тому +1

    btw it's SORT and not SHORT 🤣🤣🤣 Can't stop myself from mentioning that..

  • @sumitjindal1115
    @sumitjindal1115 3 роки тому +1

    improve your accent bhaiiii

  • @apoorv7361
    @apoorv7361 3 роки тому

    can anyone tell why this code is not working ??
    int minSwaps(vector&nums){
    // Code here
    vector help = nums;
    int ans = 0;
    sort(help.begin(),help.end());
    for(int i = 0 ; i < help.size() ; i++){
    if(help[i]!=nums[i])
    ans++;
    }
    if(ans%2==0)
    return ans/2;
    return ans/2+1;
    }

    • @srinivaskonduri7496
      @srinivaskonduri7496 3 роки тому +1

      in the question, it is asking min number of swaps. so u need to take its previous index in the algo and sort that vector of pairs and convert that sorted one into the vector given as input.

  • @ksansudeen
    @ksansudeen 3 роки тому

    please explain in English

  • @vinodthakur6783
    @vinodthakur6783 Рік тому

    chod do bhai pdhna ,
    student ka time waste nhi hoga

  • @PranavPrajapati-hd2eu
    @PranavPrajapati-hd2eu 2 роки тому

    Short nhi sort

  • @anshulvairagade1604
    @anshulvairagade1604 4 роки тому

    Bhaiya why you used v(N) ?

    • @amandeepnokhwal2977
      @amandeepnokhwal2977 4 роки тому +1

      Pta nhi ji kon sa nasha karte hai

    • @anshulvairagade1604
      @anshulvairagade1604 4 роки тому

      @@amandeepnokhwal2977 kon?
      Vo doubt clear hogaya stack overflow se

    • @amandeepnokhwal2977
      @amandeepnokhwal2977 4 роки тому

      @@anshulvairagade1604 link bhejiyo soln ka 🙂

    • @anshulvairagade1604
      @anshulvairagade1604 4 роки тому

      Solution ya vo v(N) bracket me N w use kiye vo doubt ?? Doubt ki link
      stackoverflow.com/questions/52358322/when-i-try-to-define-this-vector-pair-int-pairint-int-vpn

    • @amandeepnokhwal2977
      @amandeepnokhwal2977 4 роки тому

      @@anshulvairagade1604 thnx bro 🦄

  • @MrNeverBefore
    @MrNeverBefore 3 роки тому

    how to implement this in java ?

    • @saifulhasan2532
      @saifulhasan2532 3 роки тому +1

      ```
      class Solution
      {
      static class Pair{
      int element;
      int index;
      Pair(Integer element,Integer index){
      this.element = element;
      this.index = index;
      }
      }
      //Function to find the minimum number of swaps required to sort the array.
      public int minSwaps(int nums[])
      {
      if(nums == null || nums.length == 0 ){
      return 0;
      }
      ArrayList al = new ArrayList();
      for(int i=0;ia.element-b.element);
      int count = 0;
      for(int i=0;i

  • @testingsharma5297
    @testingsharma5297 3 роки тому

    Bhai short nahi hota h Sort hota h

  • @fullstacknoobeditor
    @fullstacknoobeditor 3 роки тому

    Sort not short

  • @bhatiasatwik
    @bhatiasatwik 5 місяців тому

    sale "Shorted" nai "sorted" hota hai

  • @dipeshsaili4468
    @dipeshsaili4468 3 роки тому

    short kardo

  • @rhythmnarula3759
    @rhythmnarula3759 3 роки тому

    bilkul bakwass 7 min ka explanation 17 minmein samjha rakha