Count Inversions | GFG Count Inversions Solution | Seaching and Sorting

Sdílet
Vložit
  • čas přidán 10. 05. 2021
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
    Have a look at our result: www.pepcoding.com/placements
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education

Komentáře • 118

  • @aryanyadav3926
    @aryanyadav3926 Před 2 lety +11

    Manisha ma'am is a legend. She gave such a wonderful explanation!

  • @SohailKhan-dq2qm
    @SohailKhan-dq2qm Před 3 měsíci +2

    i wasted my whole day to understand this problem but after that i luckily found this solution and believe me i understood 100%
    what a great explanation by mam
    thankyou so much mam

  • @vivekptl296
    @vivekptl296 Před 3 lety +18

    I was frustrating to understand merge sort, then I saw this and every thing is clear. Thank you

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

      Thankyou
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @priyanshukasaudhan786
    @priyanshukasaudhan786 Před rokem +1

    I literally wasted an hour to understand gfg code but when I watch your solution,
    I just get in one click simplest code &
    explanation is🙌

  • @ellis-pr7hh
    @ellis-pr7hh Před rokem +2

    literally great explanation ..lot better than anyone..

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

    the way you approach the question is by far the best in internet.

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

    In love with pepcoding these days.

  • @ayushijindal4898
    @ayushijindal4898 Před rokem +1

    She is a DIVA so beautifully explained. Keep doing the good work and helping students like me

  • @rajankhunt7002
    @rajankhunt7002 Před rokem +2

    Your explanting style is soo sooo soooo good and please put video on recursion and how to do though process on recursion please make video.

  • @darnavik2930
    @darnavik2930 Před rokem +1

    This is the best ever explaination ever for this

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

    Great Great Explanation. Pepcoding is the reason i love doing problem solving.

  • @darshandm8356
    @darshandm8356 Před rokem +1

    had seen many videos but was not able to understand this concept and this video made me understand in a single shot... kudos to the teaching style..... keep growing pepcoding!!!!!

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

    Kitna accha padhati ho aap..see, zero dislikes!

  • @sulemankhan523
    @sulemankhan523 Před rokem +1

    Thanks for very easy explanation of using merge sort to calculate inversion

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

    I guarantee this is the best ever explanation 😃 thanks pepcoding

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

    Hats off!! For the level of explanation 🙏🙏

  • @milangupta1161
    @milangupta1161 Před rokem +1

    great ...again i am seeing you as my trainer

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

    You made it easy. Great Explanation !!!

  • @Eklavya_Axioms
    @Eklavya_Axioms Před rokem +1

    thank you so much for a soothing explanation. Keep it up Pepcoding.

  • @anuragkhevadiya255
    @anuragkhevadiya255 Před rokem +1

    Great explaination mam i have watched a lot of videos before but couldn't understand the code but finally this video is very helpful for me ....Love the way you explain❤️❤️👍👍

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

    This channel deserve millions of subs.....awesome ♥

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

    Your explanation always outstanding 🙏🔥

  • @kewtomrao
    @kewtomrao Před 2 lety +8

    Pepcoding never dissapoints!!!Awesome explanation!!

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it
      For better experience and well organised content sign up on nados.io
      Don't forget to follow us on Instagram instagram.com/pepcoding/

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

    Thank you for this! really great explanation.

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

    Amazing explanation !! thank you so much!!!

  • @Aryansingh-fk7hy
    @Aryansingh-fk7hy Před 2 lety +1

    better,better,better than many!.Thanku mam

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

    This code failed to pass tetscases on gfg

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

    Thanks a lot...really great explanation

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

    great mem i love it your exprenation of code..

  • @raviraj-xq4ue
    @raviraj-xq4ue Před 2 lety +1

    kya smjhaya h .....behtareen

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

    Best explanation of count inversions. Great!

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad it was helpful!
      Keep learning.
      And for better experience and well organised content visit nados.pepcoding.com

  • @Ranajit-Surya99
    @Ranajit-Surya99 Před rokem +1

    plz you make more video. much clear explanation

  • @RiteshYadav-rc1np
    @RiteshYadav-rc1np Před 3 lety +2

    pepcoding always rocks , thnxx for this .

    • @Pepcoding
      @Pepcoding  Před 3 lety

      If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @nikhilaggarwal9325
    @nikhilaggarwal9325 Před 3 lety

    sir ye recursive algo hai aur humne global variable ka use kiya hai interview mei problem to nhi hogi na iss method se

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

    Best Explaination !!

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

    Thanks. it was the most difficult question

  • @shivamjadhav9393
    @shivamjadhav9393 Před rokem +1

    Excellent Video!

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

    Your explanation is very clear and outstanding.

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

  • @kanishkaverma6221
    @kanishkaverma6221 Před rokem +1

    So good... thanks a ton....

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

    Awesome !! 🔥

  • @prashantbhati8705
    @prashantbhati8705 Před rokem +1

    Best explanation 👍

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

    Shandarrrr Explanation

  • @aahanaganjewar9951
    @aahanaganjewar9951 Před rokem +1

    Thankyou ma'am!!

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

    thankyou

  • @rahulkumarsingh527
    @rahulkumarsingh527 Před rokem +1

    you are amazing

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

    Clear and excellent explaination

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thank you so much and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @sandeeppansari2944
    @sandeeppansari2944 Před rokem

    mindblowing

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

    Excellent explanation manisha ma'am!!

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it For better experience and well organised content sign up on nados.io and start learning.

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

    Best explanation...loved it 💫

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it:) To watch more content like nados.pepcoding.com

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

    really appreciate
    it help a lot .....almost die before came here

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Glad to know that you liked the content and thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

  • @siddhantsingh8721
    @siddhantsingh8721 Před rokem +2

    Best ma'am best

  • @gulshanrajput2458
    @gulshanrajput2458 Před rokem +1

    Nice..

  • @nikhilvashisht2810222
    @nikhilvashisht2810222 Před 3 lety

    Thankyou for this! really helped.

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

      Glad it helped and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

    • @nikhilvashisht2810222
      @nikhilvashisht2810222 Před 3 lety

      @@Pepcoding sure 👍

    • @yoyocontact5181
      @yoyocontact5181 Před 3 lety

      will this videos solution work for 1,9,6,4,5 ? i tried but it doesnt show correct ans it says 3 but answer is 5

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

    You are a legend.

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Thankyou for your compliment.
      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

  • @SURAJKUMAR-nl3ly
    @SURAJKUMAR-nl3ly Před rokem +1

    Such a wonderful explanation... thank you mam.. _/\_

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

    Great video! Plz also upload the video for count of smaller nodes after self. That is also done by mergesort

    • @Pepcoding
      @Pepcoding  Před 3 lety

      As soon as possible

    • @yoyocontact5181
      @yoyocontact5181 Před 3 lety

      will this videos solution work for 1,9,6,4,5 ? i tried but it doesnt show correct ans it says 3 but answer is 5

    • @humbleGuy_1729
      @humbleGuy_1729 Před 2 lety

      @@yoyocontact5181 yes

  • @PriteshRanjan30
    @PriteshRanjan30 Před rokem +1

    👍👍👍👍

  • @letsdoeverythinginoneweek9398

    nice and easy
    very good explanationn

    • @Pepcoding
      @Pepcoding  Před 2 lety

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      czcams.com/users/Pepcodingabout?view_as=subscriber

  • @sujitkumar-xs2wy
    @sujitkumar-xs2wy Před 3 lety

    Thanks🙏✨💯

  • @apurvgarg4368
    @apurvgarg4368 Před 2 lety

    simple code in 10 lines
    def count_inversions(a):
    res = 0
    counts = [0]*(len(a)+1)
    rank = { v : i+1 for i, v in enumerate(sorted(a)) }
    for x in reversed(a):
    i = rank[x] - 1
    while i:
    res += counts[i]
    i -= i & -i
    i = rank[x]
    while i

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

    thik h jiii

  • @NeerajSharma-mz4es
    @NeerajSharma-mz4es Před 3 lety

    Will this work for array having duplicates values

    • @mickyman753
      @mickyman753 Před 2 lety

      yup ,inversion just means a ele on index i>index j ele on right ,duplicates won't matter here , 4 5 4 4,here 5-4 and 5-4 are making two inversion

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

    Hatts off 🙏

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

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

    🙌👌⭐

  • @atharvakulkarni2024
    @atharvakulkarni2024 Před 3 lety

    If this que comes up in the interview and if he asks have u done this question first when i try to explain him this method after explaining the brute force method then what i should say?Bec merge sort method is not intuitive..concept used is intuitive but method is not?How should one proceed in such scenerio?

    • @freshcontent3729
      @freshcontent3729 Před 3 lety

      will this videos solution work for 1,9,6,4,5 ? i tried but it doesnt show correct ans it says 3 but answer is 5 , bro can you please try

    • @satyammishra5869
      @satyammishra5869 Před 2 lety

      @@freshcontent3729 yes it is working

  • @indranilchakraborty5949

    we can also solve it using fenwick tree....

  • @a_72_saquibsheikh44
    @a_72_saquibsheikh44 Před rokem

    At geeksForGeeks portal at different test cases this solution is not giving the desired result.

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

    Can you please explain me how to write 28 and 29 line of code in c++...plz help me

    • @manishapawar3471
      @manishapawar3471 Před 2 lety

      Use vector instead of array in cpp, vector has a size function and it is easy to use.

  • @yoyocontact5181
    @yoyocontact5181 Před 3 lety

    will this videos solution work for 1,9,6,4,5 ? i tried but it doesnt show correct ans it says 3 but answer is 5

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

    aapke white board ka ky name h
    plzz reply

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

    Thanks Mam :-)

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Hope you like the video, for better experience and well organised content visit - nados.io

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

    can we write code in c++ by returning array ?

    • @manishapawar3471
      @manishapawar3471 Před 2 lety

      Yes you can
      int* fun() {} , you have to receive this like int*arr = fun();

    • @Pepcoding
      @Pepcoding  Před 2 lety

      visit nados.pepcoding.com; post your query, there our community will help you out.

  • @meghrajgupta4560
    @meghrajgupta4560 Před 2 lety

    22:48

  • @dietpepsi7338
    @dietpepsi7338 Před 3 lety

    493. Reverse Pairs
    isi way se leetcode 493 kia pr tle aa rha hai

  • @adnaan8485
    @adnaan8485 Před 3 lety

    it took me 1 hr to understand why this video has got zero dislikes

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Glad to know that you liked the content and thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

    • @adnaan8485
      @adnaan8485 Před 3 lety

      @@Pepcoding and onemore thing i wanted to ask im currently in begin of 3rd year(B.tech) will i be able to learn every thing in 1 year and get product based companies

  • @Y_bhav
    @Y_bhav Před 5 měsíci

    why always pepcoding...

  • @TheMohit987
    @TheMohit987 Před rokem

    Your explanation is good. However, returning the array from merge sort functions is not good.
    Also taking global static variables is also not good. Ideally, a function should return the answer.
    A good company's interviewer won't accept this.
    So this could be the code without any global static variable:
    class Solution {
    int getInversionCount(int[] array) {
    return mergeSort(array, 0, array.length - 1);
    }
    private int mergeSort(int[] array, int low, int high){
    int count = 0;
    if(low < high){
    int mid = low + (high - low) / 2;
    count += mergeSort(array, low, mid);
    count += mergeSort(array, mid + 1, high);
    count += merge(array, low, mid, high);
    }
    return count;
    }
    private int merge(int array[], int low, int mid, int high){
    int inversion = 0;
    int tempArray[] = new int[high - low + 1];
    int index1 = low;
    int index2 = mid + 1;
    int arr1Size = mid + 1;
    int arr2Size = high + 1;
    int mergedArrIndex = 0;
    while(index1 < arr1Size && index2 < arr2Size){
    if(array[index1]

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

    very good explanation
    thank you @pepcoding team for such a amazing video

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it!
      For better experience, visit nados.io, where you will get well curated content and career opportunities.

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

    sir ye recursive algo hai aur humne global variable ka use kiya hai interview mei problem to nhi hogi na iss method se

  • @PawanKumar-ot2qs
    @PawanKumar-ot2qs Před 3 lety +1

    Thankyou... Really awesome explanation

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thank you so much and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

    • @yoyocontact5181
      @yoyocontact5181 Před 3 lety

      will this videos solution work for 1,9,6,4,5 ? i tried but it doesnt show correct ans it says 3 but answer is 5

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

      @@yoyocontact5181
      class Solution
      {
      // arr[]: Input Array
      // N : Size of the Array arr[]
      //Function to count inversions in the array.
      private static long count=0;
      static long inversionCount(long arr[], long N)
      {
      // Your Code Here
      long arr1[]=mergeSort(arr,0,arr.length-1);
      //System.out.println(Arrays.toString(arr1));
      return count;
      }
      private static long[] mergeSort(long arr[],long left,long right){
      if(left==right){
      long[] a=new long[1];
      a[0]=arr[(int)left];
      return a;
      }
      long mid=left+(right-left)/2;
      long leftArr[]=mergeSort(arr,left,mid);
      long rightArr[]=mergeSort(arr,mid+1,right);
      long mergedArr[]=merge(leftArr,rightArr);
      return mergedArr;
      }
      private static long[] merge(long leftA[],long rightA[]){
      long[] mArr=new long[leftA.length+rightA.length];
      int i=0,j=0,k=0;
      while(i