Counting inversions in an array

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • 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 :)

ความคิดเห็น • 184

  • @AarshSharma
    @AarshSharma 4 ปีที่แล้ว +56

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

  • @ShivamSingh-me1nb
    @ShivamSingh-me1nb 4 ปีที่แล้ว +56

    Best explanation I have seen so far

  • @VikasKumar-nb2pn
    @VikasKumar-nb2pn 4 ปีที่แล้ว +20

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

  • @tanayvartak4362
    @tanayvartak4362 3 ปีที่แล้ว +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 😊

  • @mansiagrawal4039
    @mansiagrawal4039 3 ปีที่แล้ว +4

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

  • @SinghFlex
    @SinghFlex 4 ปีที่แล้ว +7

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

  • @sainathsingineedi2922
    @sainathsingineedi2922 3 ปีที่แล้ว +7

    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)

  • @RaGa_BABA
    @RaGa_BABA 3 ปีที่แล้ว +2

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

  • @praveenchouhan6388
    @praveenchouhan6388 3 ปีที่แล้ว +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!!!!!!!!!!!!!

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Nice to hear this :)

  • @amit36666
    @amit36666 4 ปีที่แล้ว +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 3 ปีที่แล้ว +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 ปีที่แล้ว

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

    • @TJVargasS
      @TJVargasS ปีที่แล้ว

      @@aeroabrar_31 treeSet.headSet().size

  • @harshitagrawal789
    @harshitagrawal789 4 ปีที่แล้ว +8

    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!

    • @kunalpahwa7720
      @kunalpahwa7720 4 ปีที่แล้ว

      @techdose can you explain the complexity of the 3rd method?

    • @softwaredevelopment4392
      @softwaredevelopment4392 4 ปีที่แล้ว

      Ya exactly I have the same doubt ..

    • @adelikomtyagi5324
      @adelikomtyagi5324 4 ปีที่แล้ว +2

      He used distance function which take O(n) time

    • @kanishkagupta5801
      @kanishkagupta5801 3 ปีที่แล้ว

      Could you please provide its code. I am not able to find it anywhere

  • @elizabethr5161
    @elizabethr5161 ปีที่แล้ว +1

    Crystal clear explanation..

  • @deepakprajapati5064
    @deepakprajapati5064 3 ปีที่แล้ว +1

    this is called explanation with good knowledge

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 2 ปีที่แล้ว +1

    time complexity for BIT method is O(nlogn)

  • @b9944236
    @b9944236 9 หลายเดือนก่อน

    Picture explains everything, thanks.

  • @kashishsharma6809
    @kashishsharma6809 2 ปีที่แล้ว +1

    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.

  • @KathanShah03
    @KathanShah03 2 ปีที่แล้ว +1

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

  • @javadsayevand8273
    @javadsayevand8273 3 ปีที่แล้ว +1

    The Best explanation ever... 🙏🙏🙏

  • @jarno9340
    @jarno9340 3 ปีที่แล้ว +2

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

  • @sagargupta1615
    @sagargupta1615 4 ปีที่แล้ว

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

  • @sukritkapil2562
    @sukritkapil2562 4 ปีที่แล้ว +2

    Crystal clear explanation. Thanks a lot!!

  • @jpakash1999
    @jpakash1999 4 ปีที่แล้ว +5

    You are genius :-) Thank You

  • @kaushikramabhotla4635
    @kaushikramabhotla4635 2 ปีที่แล้ว

    The best explanation for this problem 👏 👌

  • @yuktykhandelia5312
    @yuktykhandelia5312 3 ปีที่แล้ว +1

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

  • @AyushJain-ot9yv
    @AyushJain-ot9yv 4 ปีที่แล้ว +4

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

  • @vipulkrishna19
    @vipulkrishna19 5 ปีที่แล้ว +3

    very very good explanation

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 4 ปีที่แล้ว +2

    very nice explaination ever seen .thanks a lot

  • @shubhammishra3783
    @shubhammishra3783 4 ปีที่แล้ว +2

    very clear explanation thank you

  • @changjeffreysinto3872
    @changjeffreysinto3872 2 ปีที่แล้ว

    Clear explanation dude

  • @vasujain1970
    @vasujain1970 3 ปีที่แล้ว +2

    This was an amazing explanation! Thank you!

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว +1

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

  • @PizzaGamingTheReal
    @PizzaGamingTheReal 4 ปีที่แล้ว +1

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

  • @biswamohandwari6460
    @biswamohandwari6460 4 ปีที่แล้ว +6

    Man I got understood everything, I saw there

  • @Joyddep
    @Joyddep 3 ปีที่แล้ว +2

    Great explanation!

  • @lifecodesher5818
    @lifecodesher5818 4 ปีที่แล้ว +2

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Software Developement Engineer.

  • @anjana1700
    @anjana1700 4 ปีที่แล้ว +2

    Awesome Sir!
    Sir please make a video on BIT also

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Yea...will make it.

  • @divyareddy7622
    @divyareddy7622 ปีที่แล้ว

    thankk you bhaiya!! amazing

  • @karanshah838
    @karanshah838 3 ปีที่แล้ว +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 3 ปีที่แล้ว

      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 3 ปีที่แล้ว

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

    • @karanshah838
      @karanshah838 3 ปีที่แล้ว

      @@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)

  • @kollivenkatamadhukar5059
    @kollivenkatamadhukar5059 4 ปีที่แล้ว +1

    Excellent Explanation

  • @todo1553
    @todo1553 2 ปีที่แล้ว +1

    Thanks for the explanation

  • @purushottamkumar3140
    @purushottamkumar3140 2 ปีที่แล้ว

    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

  • @Navneetkumar-os5cl
    @Navneetkumar-os5cl 4 ปีที่แล้ว +1

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

  • @sunwado
    @sunwado 3 ปีที่แล้ว +1

    Precise and too the point :)

  • @shubhamprashar7676
    @shubhamprashar7676 3 ปีที่แล้ว +1

    Why do we need to merge the partitions?

  • @anitasingh-jw1lc
    @anitasingh-jw1lc 2 ปีที่แล้ว

    merge sort is very understandi all us

  • @shobhitbajaj9667
    @shobhitbajaj9667 4 ปีที่แล้ว +1

    Wow what a great explanation 👍

  • @siddhantjaiswal4231
    @siddhantjaiswal4231 4 ปีที่แล้ว +3

    pls discuss the BIT method

  • @jakubfraczek1208
    @jakubfraczek1208 ปีที่แล้ว

    THANK YOU SO MUUUUUUUUCH

  • @hitaishidas4955
    @hitaishidas4955 4 ปีที่แล้ว +1

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

    • @prateeksinghal630
      @prateeksinghal630 4 ปีที่แล้ว +1

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

  • @siddhantsingh8721
    @siddhantsingh8721 ปีที่แล้ว

    best explanation

    • @techdose4u
      @techdose4u  ปีที่แล้ว

      Glad you think so!

  • @UnKnown-id7ih
    @UnKnown-id7ih 3 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Yea I am doing it from March 2019.

  • @SudhanshuKumar-jl8iw
    @SudhanshuKumar-jl8iw 3 ปีที่แล้ว +1

    great explanation

  • @22.jananidileepan44
    @22.jananidileepan44 ปีที่แล้ว

    Superb

  • @vivekshaw2437
    @vivekshaw2437 4 ปีที่แล้ว +4

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +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.

  • @dayanandraut5660
    @dayanandraut5660 3 ปีที่แล้ว

    merge sort technique is the best for this problem

  • @excessreactant9045
    @excessreactant9045 4 ปีที่แล้ว +1

    Thanks for this my book didnt show this well

  • @Samir-ll3kj
    @Samir-ll3kj 3 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +2

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

  • @patilsoham
    @patilsoham 4 ปีที่แล้ว +1

    Helped a lot!
    Thank you!

  • @priyasharma3549
    @priyasharma3549 2 ปีที่แล้ว

    thank you so much

  • @diptamoy12
    @diptamoy12 ปีที่แล้ว

    Atlast understood ❤❤

  • @tusharkumar3277
    @tusharkumar3277 4 ปีที่แล้ว +1

    nice video ,really helpful

  • @manavmalhotra8513
    @manavmalhotra8513 4 ปีที่แล้ว

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

  • @satyamgupta5234
    @satyamgupta5234 2 ปีที่แล้ว

    we can use stack also

  • @MohitKumar-rq8hv
    @MohitKumar-rq8hv 2 ปีที่แล้ว

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

  • @sharuk3545
    @sharuk3545 3 ปีที่แล้ว

    Awesome 👍

  • @anjalipatel5643
    @anjalipatel5643 หลายเดือนก่อน

    Can you please explain this question in single file programming.

  • @yyndsai
    @yyndsai 3 ปีที่แล้ว

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

  • @sutirthasadhukhan78
    @sutirthasadhukhan78 3 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      I will do it after DP.

  • @ayushkawatra7436
    @ayushkawatra7436 3 ปีที่แล้ว

    really helped a lot

  • @kislayapant6119
    @kislayapant6119 4 ปีที่แล้ว +2

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Sure.... When I get time I will

    • @kislayapant6119
      @kislayapant6119 4 ปีที่แล้ว +1

      @@techdose4u thanks sir

  • @_PAYALGAIKWAD
    @_PAYALGAIKWAD 2 ปีที่แล้ว

    thnx a lot

  • @gokulnaathb2627
    @gokulnaathb2627 2 ปีที่แล้ว +1

    Multiset😍

  • @hadeerzayat355
    @hadeerzayat355 3 ปีที่แล้ว

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

  • @mukulrustagi9404
    @mukulrustagi9404 3 ปีที่แล้ว +1

    Awesome

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 ปีที่แล้ว

    Nice explaination, BIT method too (:

  • @bhuvaneshwaranr5566
    @bhuvaneshwaranr5566 4 ปีที่แล้ว +3

    could you give the code for method 2?

    • @nemaligirisai2896
      @nemaligirisai2896 3 ปีที่แล้ว

      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

  • @spetsnaz_2
    @spetsnaz_2 5 ปีที่แล้ว +1

    Last method was 💖

    • @techdose4u
      @techdose4u  5 ปีที่แล้ว +1

      😂 Using multiset is the easiest :P

  • @shivanshujaiswal7635
    @shivanshujaiswal7635 4 ปีที่แล้ว +2

    Hi please explain BIT method

  • @ankitsinghgautam1274
    @ankitsinghgautam1274 4 ปีที่แล้ว +1

    Thanks

  • @kishalaybhattacharya8312
    @kishalaybhattacharya8312 3 ปีที่แล้ว

    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

  • @dayanandraut5660
    @dayanandraut5660 2 ปีที่แล้ว

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

  • @AliObaid1992
    @AliObaid1992 4 ปีที่แล้ว +1

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

  • @rajneeshyadav9728
    @rajneeshyadav9728 4 ปีที่แล้ว +1

    can you explain the BIT method

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

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

  • @TomerBenDavid
    @TomerBenDavid 4 ปีที่แล้ว +1

    Which software do you use for the anntoations?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Ink2go :)

    • @TomerBenDavid
      @TomerBenDavid 4 ปีที่แล้ว

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

  • @AnkitKumarCoder
    @AnkitKumarCoder 3 ปีที่แล้ว +1

    From where I get the code?

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      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.

  • @sayantangupta1403
    @sayantangupta1403 4 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      I will explain the methods in detail later.

    • @sayantangupta1403
      @sayantangupta1403 4 ปีที่แล้ว

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

  • @PhenomenalInitiations
    @PhenomenalInitiations 3 ปีที่แล้ว

    getting TLE with set

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf 3 ปีที่แล้ว

    multiset me minus nhi hota h sir

  • @schan263
    @schan263 10 หลายเดือนก่อน

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

  • @shivangitomar5557
    @shivangitomar5557 3 ปีที่แล้ว

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

  • @priyabratasingha174
    @priyabratasingha174 2 ปีที่แล้ว +2

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

    • @soumyadeepdash4805
      @soumyadeepdash4805 2 ปีที่แล้ว +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)

  • @softwaredevelopment4392
    @softwaredevelopment4392 4 ปีที่แล้ว

    Please explain BIT

  • @nishayadav9988
    @nishayadav9988 3 ปีที่แล้ว +1

    sir please also provide a code of this problem

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf 3 ปีที่แล้ว

    difference kaise nikale

  • @mtr2936
    @mtr2936 3 ปีที่แล้ว +1

    Sir can you share the source code

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Please try to search in comment section

  • @ShivamGupta-cx3hy
    @ShivamGupta-cx3hy 3 ปีที่แล้ว +2

    you should change your name to Placement Cracker

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Let it be techdose :P

    • @ShivamGupta-cx3hy
      @ShivamGupta-cx3hy 3 ปีที่แล้ว

      @@techdose4u Thank you So much ,Sir you are a great help .

  • @aneeshkulkarni1739
    @aneeshkulkarni1739 4 ปีที่แล้ว +2

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      :P

    • @aneeshkulkarni1739
      @aneeshkulkarni1739 4 ปีที่แล้ว +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  4 ปีที่แล้ว

      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 4 ปีที่แล้ว

      How about "count of smaller numbers after self" ?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I guess I should have explained that in video itself 😅

  • @margolikesbees
    @margolikesbees 3 ปีที่แล้ว

    :)

  • @purushottamkumar3140
    @purushottamkumar3140 2 ปีที่แล้ว

    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

  • @rohitpal7739
    @rohitpal7739 4 ปีที่แล้ว

    maal