Counting inversions in an array

Sdílet
Vložit
  • čas přidán 1. 09. 2019
  • This video explains how to find number of inversions in an array using 3 methods. The video first explains what is inversion and its conditions followed by solutions. The first solution is a brute-force method. The second solution is based on merge sort technique. The third technique is based on multiset using c++ STL. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

Komentáře • 183

  • @AarshSharma
    @AarshSharma Před 3 lety +55

    I always thought it was an extremely hard problem. This explanation made it look so easy. Thanks.

  • @ShivamSingh-me1nb
    @ShivamSingh-me1nb Před 4 lety +55

    Best explanation I have seen so far

  • @tanayvartak4362
    @tanayvartak4362 Před 3 lety +19

    This is the best video to understand the merge sort method for count inversion. Explanation was really very clear and straight forward. Thanks a lot 😊

  • @VikasKumar-nb2pn
    @VikasKumar-nb2pn Před 4 lety +18

    One best thing is that
    U always take tough example which explain all the possible situation....
    Awesome explanation..
    Thanks sir...

  • @SinghFlex
    @SinghFlex Před 4 lety +7

    Clear and crisp explanation. Thankyou for creating such a good stuff.

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

    this is called explanation with good knowledge

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

    Very clear explained! Thanks. That's what I needed!

  • @kaushikramabhotla4635
    @kaushikramabhotla4635 Před 2 lety

    The best explanation for this problem 👏 👌

  • @sagargupta1615
    @sagargupta1615 Před 3 lety

    you are really tech dose....ur explanation is very good.

  • @sukritkapil2562
    @sukritkapil2562 Před 4 lety +2

    Crystal clear explanation. Thanks a lot!!

  • @b9944236
    @b9944236 Před 6 měsíci

    Picture explains everything, thanks.

  • @shubhammishra3783
    @shubhammishra3783 Před 4 lety +2

    very clear explanation thank you

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

    After seeing this video this problem became a very easy problem. Thank you

  • @AyushJain-ot9yv
    @AyushJain-ot9yv Před 4 lety +4

    Best Channel Ever! You are doing good work. :)

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

    This was an amazing explanation! Thank you!

  • @patilsoham
    @patilsoham Před 4 lety +1

    Helped a lot!
    Thank you!

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

    The Best explanation ever... 🙏🙏🙏

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

    bhai 1 number....I always prefer your video when I get stuck on any problem.

  • @changjeffreysinto3872
    @changjeffreysinto3872 Před 2 lety

    Clear explanation dude

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

    Precise and too the point :)

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

    BEST EXPLAINATION SEEN SO FAR, UR VIDEO IS GENERALLY THE LAST VIDEO I SEE BECAUSE IT MAKES ALL CLEAR, PLEASE CONSIDER CAPS AS A SALUTE TO YOU NOT HARSH!!!!!!!!!!!!!

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

    Thanks man, explaining step by step is the clearest method. Now I got this algorithm.

  • @divyareddy7622
    @divyareddy7622 Před rokem

    thankk you bhaiya!! amazing

  • @elizabethr5161
    @elizabethr5161 Před rokem +1

    Crystal clear explanation..

  • @jpakash1999
    @jpakash1999 Před 4 lety +5

    You are genius :-) Thank You

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

    Wow what a great explanation 👍

  • @ayushkawatra7436
    @ayushkawatra7436 Před 3 lety

    really helped a lot

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

    Nice explanation sir....you are doing a great job

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

    Great explanation!

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 Před 4 lety +2

    very nice explaination ever seen .thanks a lot

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

    Thanks for the explanation

  • @sharuk3545
    @sharuk3545 Před 3 lety

    Awesome 👍

  • @sainathsingineedi2922
    @sainathsingineedi2922 Před 3 lety +6

    I think it's O(nlogn) for Fenwick trees too correct me if I'm wrong
    O(n) traversing
    -> O(logn) for quering the sum
    -> O(logn) for updating
    Overall O(nlogn)

  • @priyasharma3549
    @priyasharma3549 Před 2 lety

    thank you so much

  • @kollivenkatamadhukar5059
    @kollivenkatamadhukar5059 Před 4 lety +1

    Excellent Explanation

  • @amit36666
    @amit36666 Před 3 lety +13

    In the 3rd method, the std::distance time complexity on std::multiset is O(n), so the overall time complexity is O(n^2)

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

      I don’t know c++, but Java has treeset which can be used to solve the problem in O(n logn). We cannot achieve this time complexity using hash set. Tree set basically is an implementation of red black trees. Also using fenwick trees will take O(n log n) instead of O(n) as told in the video.

    • @aeroabrar_31
      @aeroabrar_31 Před rokem

      Hey Man!
      How to access the indexOf an elem in TreeSet ..?

    • @TJVargasS
      @TJVargasS Před rokem

      @@aeroabrar_31 treeSet.headSet().size

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

    very very good explanation

  • @tusharkumar3277
    @tusharkumar3277 Před 4 lety +1

    nice video ,really helpful

  • @jakubfraczek1208
    @jakubfraczek1208 Před rokem

    THANK YOU SO MUUUUUUUUCH

  • @diptamoymitra7486
    @diptamoymitra7486 Před rokem

    Atlast understood ❤❤

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi Před 3 lety

    Nice explaination, BIT method too (:

  • @siddhantsingh8721
    @siddhantsingh8721 Před rokem

    best explanation

  • @22.jananidileepan44
    @22.jananidileepan44 Před rokem

    Superb

  • @excessreactant9045
    @excessreactant9045 Před 4 lety +1

    Thanks for this my book didnt show this well

  • @SudhanshuKumar-jl8iw
    @SudhanshuKumar-jl8iw Před 3 lety +1

    great explanation

  • @dayanandraut5660
    @dayanandraut5660 Před 3 lety

    merge sort technique is the best for this problem

  • @anitasingh-jw1lc
    @anitasingh-jw1lc Před rokem

    merge sort is very understandi all us

  • @_PAYALGAIKWAD
    @_PAYALGAIKWAD Před 2 lety

    thnx a lot

  • @anjana1700
    @anjana1700 Před 4 lety +2

    Awesome Sir!
    Sir please make a video on BIT also

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

    Thank You for the explanation. Where can I find Method - 5 explanation?

  • @biswamohandwari6460
    @biswamohandwari6460 Před 4 lety +6

    Man I got understood everything, I saw there

  • @harshitagrawal789
    @harshitagrawal789 Před 4 lety +7

    I think the time complexity of upper_bound in a multiset is log(n) so the third method can be O(nlogn). Not sure though!

  • @kashishsharma6809
    @kashishsharma6809 Před 2 lety

    by BIT method also it takes O(nlogN) where N is maximum no present in array. if anyone trying BIT method check for method where there are negative integers also. And if by chance its floating array then merge sort method is best.

  • @AliObaid1992
    @AliObaid1992 Před 4 lety +1

    انت راقي احبك😘

  • @spetsnaz_2
    @spetsnaz_2 Před 4 lety +1

    Last method was 💖

    • @techdose4u
      @techdose4u  Před 4 lety +1

      😂 Using multiset is the easiest :P

  • @Navneetkumar-os5cl
    @Navneetkumar-os5cl Před 3 lety +1

    Sir in BiT updating value and getting prefix sum takes O(log(n)) time then how it is supposed to be O(n)

  • @UnKnown-id7ih
    @UnKnown-id7ih Před 3 lety +1

    It would be very good if you shown code also,by the way it is fabulous.

    • @techdose4u
      @techdose4u  Před 3 lety

      Yea I am doing it from March 2019.

  • @manavmalhotra8513
    @manavmalhotra8513 Před 3 lety

    a[i]>a[j] and j>i , this is the inversion

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf Před rokem

    time complexity for BIT method is O(nlogn)

  • @yuktykhandelia5312
    @yuktykhandelia5312 Před 3 lety

    The multiset method doesn't satisfy the time constraint as the distance method takes O(n) for multisets.

  • @Samir-ll3kj
    @Samir-ll3kj Před 3 lety +1

    Please make a video on AVL tree and BIT. It would be very helpful.

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

      It will come later when I resume Segment tree and TREE.

  • @kishalaybhattacharya8312

    do u have any video on inversion in 2D array, in my code the time complexity is O(n^4), I want to reduce it

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

    I think the MultiSet implementation is O(nlogn)
    upper_bound => O(logn) in multiset
    We do upper_bound 'n' times
    Therefore, O(nlogn)

    • @vetald1979
      @vetald1979 Před 3 lety

      Thinking it N*Log(N)*Log(N) - to insert all element in a set is N*Log(N) - N-elements * Log N to insert * get the index for each insertion is LogN

    • @aesthetic_vibes_404
      @aesthetic_vibes_404 Před 2 lety

      N² bcz a loop is outside for insertion O(n) and distance stl gonna take O(n) for calucating distance.
      Hence N²

    • @karanshah838
      @karanshah838 Před 2 lety

      @@aesthetic_vibes_404 Upper bound will give index and that can be used to calculate distance. So calculating distance is just O(log n) and we calculate the distance for 'n' elements. So O(nlogn)

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

    Why do we need to merge the partitions?

  • @yyndsai
    @yyndsai Před 3 lety

    So it's like checking if they are sorted or not?

  • @MohitKumar-rq8hv
    @MohitKumar-rq8hv Před 2 lety

    method 2-merge sort 4:30
    method 3-multiset 14:30

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

    Awesome

  • @satyamgupta5234
    @satyamgupta5234 Před 2 lety

    we can use stack also

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

    Thanks

  • @hadeerzayat355
    @hadeerzayat355 Před 2 lety

    Is the second method based on the merge sort called Piggybacking on merge sort also?

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

    pls discuss the BIT method

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

    I am very curious you make very good videos..what do you do full time?

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

    Multiset😍

  • @vivekshaw2437
    @vivekshaw2437 Před 4 lety +4

    can you make a video for the inversion count using BIT??

    • @techdose4u
      @techdose4u  Před 4 lety +12

      I will make it once I cover BIT. For now I am covering graph. After that I can. Actually everyone is requesting to teach different stuffs, so it's becoming very difficult to switch. As soon as i upload some videos on graph interview prep then I will switch to your idea of BIT and other DS :) I hope you understand.

  • @softwaredevelopment4392

    Please explain BIT

  • @TomerBenDavid
    @TomerBenDavid Před 4 lety +1

    Which software do you use for the anntoations?

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Ink2go :)

    • @TomerBenDavid
      @TomerBenDavid Před 4 lety

      @@techdose4u thanks! may I ask which pad do you use to do the actual writing?

  • @hitaishidas4955
    @hitaishidas4955 Před 4 lety +1

    Can you please tell me the Space Complexity of the 2nd method??

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

      space complexity is actually O(n) like merge sort since you need extra array to store the numbers in sorted order

  • @PhenomenalInitiations
    @PhenomenalInitiations Před 3 lety

    getting TLE with set

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

    could you give the code for method 2?

    • @nemaligirisai2896
      @nemaligirisai2896 Před 3 lety

      import java.lang.*;
      import java.io.*;
      class GFG {
      public static void main (String[] args){
      Scanner sc=new Scanner(System.in);
      int t=sc.nextInt();
      while(t-->0){
      int n=sc.nextInt();
      int arr[]=new int[n];
      for(int i=0;i

  • @kislayapant6119
    @kislayapant6119 Před 4 lety +2

    Sir can u pls provide the explanation for Binary indexed method for the same problem

  • @shivanshujaiswal7635
    @shivanshujaiswal7635 Před 4 lety +2

    Hi please explain BIT method

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

    can you explain the BIT method

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

      Yea....I will cover this problem when I cover BIT

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

    Sir I can't apply Fenwick tree properly. Please apply Fenwick tree to solve this problem .

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

    From where I get the code?

    • @techdose4u
      @techdose4u  Před 3 lety

      I dint provide code for this. This is an old video. Back then I was not sharing code. Sorry. But I provide all codes now.

  • @purushottamkumar3140
    @purushottamkumar3140 Před 2 lety

    Code Using Multiset
    ```
    long long int inversionCount(long long arr[], long long N)
    {
    long long int ans=0;
    multisetm;
    for( long long int i = 0; i

  • @sayantangupta1403
    @sayantangupta1403 Před 4 lety +1

    Sir, can you please provide the code for all the methods and explain all the methods in another video. Please sir.

    • @techdose4u
      @techdose4u  Před 4 lety +2

      CODE LINK: www.geeksforgeeks.org/counting-inversions/

    • @techdose4u
      @techdose4u  Před 4 lety +2

      I will explain the methods in detail later.

    • @sayantangupta1403
      @sayantangupta1403 Před 4 lety

      @@techdose4u You are doing a great job sir. Could you please share your FB Id so that i can connect.

  • @dayanandraut5660
    @dayanandraut5660 Před 2 lety

    why is this solution not working for gfg and leetcode problems?

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

    I think in the method 3 ,the time complexity should be o(nlogn).

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

      No, because the distance function has linear complexity O(n), and we are calculating this for every index, so in the worst case time complexity will be O(n^2)

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

    sir please also provide a code of this problem

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf Před 3 lety

    difference kaise nikale

  • @shivangitomar5557
    @shivangitomar5557 Před 3 lety

    3rd method should be O(nlogn) right? ( we dont need to use "distance")

  • @margolikesbees
    @margolikesbees Před 3 lety

    :)

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf Před 3 lety

    multiset me minus nhi hota h sir

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

    Sir can you share the source code

    • @techdose4u
      @techdose4u  Před 3 lety

      Please try to search in comment section

  • @schan263
    @schan263 Před 8 měsíci

    I know the solution but not the intuition that leads to merge sort.

  • @ShivamGupta-cx3hy
    @ShivamGupta-cx3hy Před 3 lety +2

    you should change your name to Placement Cracker

  • @aneeshkulkarni1739
    @aneeshkulkarni1739 Před 4 lety +2

    At 3:10 you accidentally said 4 is greater than 6 hence there is inversion.
    Got confused for a bit 😛

    • @techdose4u
      @techdose4u  Před 4 lety

      :P

    • @aneeshkulkarni1739
      @aneeshkulkarni1739 Před 4 lety +1

      Great explanation!
      Could you also make a video about how to get the number of inversions per element in the original array?

    • @techdose4u
      @techdose4u  Před 4 lety

      Sure.....but can you suggest me a proper title for that. That will be helpful to see if users need it or not.

    • @aneeshkulkarni1739
      @aneeshkulkarni1739 Před 4 lety

      How about "count of smaller numbers after self" ?

    • @techdose4u
      @techdose4u  Před 4 lety

      I guess I should have explained that in video itself 😅

  • @rohitpal7739
    @rohitpal7739 Před 4 lety

    maal

  • @purushottamkumar3140
    @purushottamkumar3140 Před 2 lety

    Code Using Merge Sort
    long long int inversionCount(long long arr[], long long n)
    {
    return mergeSort(arr,0,n-1);
    }
    long long int mergeSort(long long arr[],long long low,long long high)
    {
    long long res=0;
    if(low

  • @theSilentPsycho
    @theSilentPsycho Před 4 lety +1

    best explanation