Counting inversions in an array

Поделиться
HTML-код
  • Опубликовано: 27 дек 2024

Комментарии • 184

  • @AarshSharma
    @AarshSharma 4 года назад +58

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

  • @ShivamSingh-me1nb
    @ShivamSingh-me1nb 5 лет назад +57

    Best explanation I have seen so far

  • @tanayvartak4362
    @tanayvartak4362 4 года назад +20

    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 4 года назад +21

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

  • @SinghFlex
    @SinghFlex 4 года назад +7

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

  • @RaGa_BABA
    @RaGa_BABA 3 года назад +2

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

  • @mansiagrawal4039
    @mansiagrawal4039 4 года назад +5

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

  • @praveenchouhan6388
    @praveenchouhan6388 4 года назад +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!!!!!!!!!!!!!

  • @kaushikramabhotla4635
    @kaushikramabhotla4635 2 года назад

    The best explanation for this problem 👏 👌

  • @sagargupta1615
    @sagargupta1615 4 года назад

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

  • @elizabethr5161
    @elizabethr5161 Год назад +1

    Crystal clear explanation..

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 2 года назад +1

    time complexity for BIT method is O(nlogn)

  • @deepakprajapati5064
    @deepakprajapati5064 4 года назад +1

    this is called explanation with good knowledge

  • @javadsayevand8273
    @javadsayevand8273 3 года назад +1

    The Best explanation ever... 🙏🙏🙏

  • @AyushJain-ot9yv
    @AyushJain-ot9yv 4 года назад +4

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

  • @jarno9340
    @jarno9340 3 года назад +2

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

  • @sukritkapil2562
    @sukritkapil2562 4 года назад +2

    Crystal clear explanation. Thanks a lot!!

  • @b9944236
    @b9944236 Год назад

    Picture explains everything, thanks.

  • @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 4 года назад +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 2 года назад

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

    • @TJVargasS
      @TJVargasS Год назад

      @@aeroabrar_31 treeSet.headSet().size

  • @vasujain1970
    @vasujain1970 3 года назад +2

    This was an amazing explanation! Thank you!

  • @paragroy5359
    @paragroy5359 3 года назад +1

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

  • @sainathsingineedi2922
    @sainathsingineedi2922 4 года назад +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)

  • @changjeffreysinto3872
    @changjeffreysinto3872 2 года назад

    Clear explanation dude

  • @todo1553
    @todo1553 3 года назад +1

    Thanks for the explanation

  • @harshitagrawal789
    @harshitagrawal789 5 лет назад +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

  • @kashishsharma6809
    @kashishsharma6809 3 года назад +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.

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 4 года назад +2

    very nice explaination ever seen .thanks a lot

  • @shubhammishra3783
    @shubhammishra3783 5 лет назад +2

    very clear explanation thank you

  • @jpakash1999
    @jpakash1999 4 года назад +5

    You are genius :-) Thank You

  • @vipulkrishna19
    @vipulkrishna19 5 лет назад +3

    very very good explanation

  • @Joyddep
    @Joyddep 3 года назад +2

    Great explanation!

  • @shobhitbajaj9667
    @shobhitbajaj9667 4 года назад +1

    Wow what a great explanation 👍

  • @biswamohandwari6460
    @biswamohandwari6460 4 года назад +6

    Man I got understood everything, I saw there

  • @KathanShah03
    @KathanShah03 2 года назад +1

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

  • @divyareddy7622
    @divyareddy7622 2 года назад

    thankk you bhaiya!! amazing

  • @kollivenkatamadhukar5059
    @kollivenkatamadhukar5059 4 года назад +1

    Excellent Explanation

  • @anjana1700
    @anjana1700 4 года назад +2

    Awesome Sir!
    Sir please make a video on BIT also

  • @PizzaGamingTheReal
    @PizzaGamingTheReal 4 года назад +1

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

  • @sunwado
    @sunwado 4 года назад +1

    Precise and too the point :)

  • @yuktykhandelia5312
    @yuktykhandelia5312 4 года назад +1

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

  • @diptamoy12
    @diptamoy12 Год назад

    Atlast understood ❤❤

  • @SudhanshuKumar-jl8iw
    @SudhanshuKumar-jl8iw 4 года назад +1

    great explanation

  • @tusharkumar3277
    @tusharkumar3277 4 года назад +1

    nice video ,really helpful

  • @patilsoham
    @patilsoham 4 года назад +1

    Helped a lot!
    Thank you!

  • @shubhamprashar7676
    @shubhamprashar7676 3 года назад +1

    Why do we need to merge the partitions?

  • @dayanandraut5660
    @dayanandraut5660 3 года назад

    merge sort technique is the best for this problem

  • @sharuk3545
    @sharuk3545 3 года назад

    Awesome 👍

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

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

  • @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.

  • @anitasingh-jw1lc
    @anitasingh-jw1lc 2 года назад

    merge sort is very understandi all us

  • @manavmalhotra8513
    @manavmalhotra8513 4 года назад

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

  • @MohitKumar-rq8hv
    @MohitKumar-rq8hv 2 года назад

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

  • @excessreactant9045
    @excessreactant9045 4 года назад +1

    Thanks for this my book didnt show this well

  • @yyndsai
    @yyndsai 3 года назад

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

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

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 года назад

    Nice explaination, BIT method too (:

  • @UnKnown-id7ih
    @UnKnown-id7ih 4 года назад +1

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

    • @techdose4u
      @techdose4u  4 года назад

      Yea I am doing it from March 2019.

  • @theSilentPsycho
    @theSilentPsycho 5 лет назад +1

    best explanation

  • @hadeerzayat355
    @hadeerzayat355 3 года назад

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

  • @AliObaid1992
    @AliObaid1992 4 года назад +1

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

  • @22.jananidileepan44
    @22.jananidileepan44 2 года назад

    Superb

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

  • @siddhantjaiswal4231
    @siddhantjaiswal4231 4 года назад +3

    pls discuss the BIT method

  • @Samir-ll3kj
    @Samir-ll3kj 4 года назад +1

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

    • @techdose4u
      @techdose4u  4 года назад +2

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

  • @jakubfraczek1208
    @jakubfraczek1208 Год назад

    THANK YOU SO MUUUUUUUUCH

  • @ayushkawatra7436
    @ayushkawatra7436 4 года назад

    really helped a lot

  • @dayanandraut5660
    @dayanandraut5660 3 года назад

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

  • @priyasharma3549
    @priyasharma3549 2 года назад

    thank you so much

  • @sutirthasadhukhan78
    @sutirthasadhukhan78 4 года назад +1

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

  • @vivekshaw2437
    @vivekshaw2437 5 лет назад +4

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

    • @techdose4u
      @techdose4u  5 лет назад +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.

  • @spetsnaz_2
    @spetsnaz_2 5 лет назад +1

    Last method was 💖

    • @techdose4u
      @techdose4u  5 лет назад +1

      😂 Using multiset is the easiest :P

  • @satyamgupta5234
    @satyamgupta5234 3 года назад

    we can use stack also

  • @anjalipatel5643
    @anjalipatel5643 5 месяцев назад

    Can you please explain this question in single file programming.

  • @mukulrustagi9404
    @mukulrustagi9404 3 года назад +1

    Awesome

  • @TomerBenDavid
    @TomerBenDavid 5 лет назад +1

    Which software do you use for the anntoations?

    • @techdose4u
      @techdose4u  5 лет назад +1

      Ink2go :)

    • @TomerBenDavid
      @TomerBenDavid 5 лет назад

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

  • @bhuvaneshwaranr5566
    @bhuvaneshwaranr5566 5 лет назад +3

    could you give the code for method 2?

    • @nemaligirisai2896
      @nemaligirisai2896 4 года назад

      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 5 лет назад +2

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

  • @rajneeshyadav9728
    @rajneeshyadav9728 4 года назад +1

    can you explain the BIT method

    • @techdose4u
      @techdose4u  4 года назад +1

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

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

  • @gokulnaathb2627
    @gokulnaathb2627 3 года назад +1

    Multiset😍

  • @_PAYALGAIKWAD
    @_PAYALGAIKWAD 2 года назад

    thnx a lot

  • @shivanshujaiswal7635
    @shivanshujaiswal7635 4 года назад +2

    Hi please explain BIT method

  • @ankitsinghgautam1274
    @ankitsinghgautam1274 4 года назад +1

    Thanks

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf 3 года назад

    multiset me minus nhi hota h sir

  • @sayantangupta1403
    @sayantangupta1403 5 лет назад +1

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

    • @techdose4u
      @techdose4u  5 лет назад +2

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

    • @techdose4u
      @techdose4u  5 лет назад +2

      I will explain the methods in detail later.

    • @sayantangupta1403
      @sayantangupta1403 5 лет назад

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

  • @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 😅

  • @shivangitomar5557
    @shivangitomar5557 4 года назад

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

  • @nishayadav9988
    @nishayadav9988 3 года назад +1

    sir please also provide a code of this problem

  • @PhenomenalInitiations
    @PhenomenalInitiations 3 года назад

    getting TLE with set

  • @softwaredevelopment4392
    @softwaredevelopment4392 4 года назад

    Please explain BIT

  • @ROSHANKUMAR-rl6bf
    @ROSHANKUMAR-rl6bf 3 года назад

    difference kaise nikale

  • @priyabratasingha174
    @priyabratasingha174 3 года назад +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)

  • @mtr2936
    @mtr2936 4 года назад +1

    Sir can you share the source code

    • @techdose4u
      @techdose4u  4 года назад

      Please try to search in comment section

  • @schan263
    @schan263 Год назад

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

  • @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 .

  • @margolikesbees
    @margolikesbees 3 года назад

    :)

  • @rohitpal7739
    @rohitpal7739 4 года назад

    maal

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