Count Inversions in an Array | Brute and Optimal

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

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

  • @takeUforward
    @takeUforward  Год назад +27

    Please watch our new video on the same topic: ruclips.net/video/AseUmwVNaoY/видео.html

    • @namannema3349
      @namannema3349 Год назад +5

      this link take u to the same video we are watching

    • @yogeshkaushik8316
      @yogeshkaushik8316 10 месяцев назад +21

      @@namannema3349 thats what recursion means

    • @user-ic7xs9vh7i
      @user-ic7xs9vh7i 5 месяцев назад +2

      @@yogeshkaushik8316 😆

  • @anuplohar23
    @anuplohar23 Год назад +39

    Best count inversion video on RUclips, your method of teaching is very best that it gets me understand very easily 🌟🌟🌟

  • @satvrii
    @satvrii Год назад +161

    Nowadaya strivers voice has become so calm and soft 😅❤

  • @attackgaming9940
    @attackgaming9940 5 месяцев назад +24

    Making the complex problem looks simpler is a kind of skill that striver only has🛐

  • @_AASHISHSHAH
    @_AASHISHSHAH Год назад +22

    this is the best playlist in world. thank you stiver for your effort.

  • @Manishgupta200
    @Manishgupta200 Год назад +31

    I'm trying this problem and solved it by myself by taking count as global variable.. But you taught us in a vary optimal way without taking count as a global variable. Really best optimal approach. Thankyou ❤

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

      whats the complexity of your solution?
      can you share it?

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

      @@bishakhdutta8427 same as merger sort merge algorithm

  • @bmishra98
    @bmishra98 6 месяцев назад +6

    Completed the playlist. This was the best recursion playlist I ever went through. Thanks a lot Striver.

  • @gajendrakumar8271
    @gajendrakumar8271 9 месяцев назад +6

    Finally completed this recursion playlist and Thanks a lot striver for great explanation throughout and patiently drawing recursion tree. patience is the key to solve and teach anything . And you have it man and you are teaching that too. Thanks a lot again

  • @gautam3945
    @gautam3945 6 дней назад

    Yet another great video Striver. Thank You!! I'll definetly support this channel from my first salary if I get placed.

  • @senseiAree
    @senseiAree Год назад +10

    Striver your voice is very soothing and calm bro ❤.... I use it to sleep at night AND study... and I don't feel sleepy

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial Год назад +17

    The best count inversion video on RUclips.. Thanks a lot Raj.. stay blessed❤

  • @vaibhav56
    @vaibhav56 6 месяцев назад +6

    I was struggling with the solution but as soon as you mentioned merge sort it clicked in my mind

  • @MehtabReviews
    @MehtabReviews Год назад +17

    I usually don't comment but wanted to say that just subbed your channel. This is the best explanation I've ever got. THANKS A LOT:>)

  • @cinime
    @cinime Год назад +17

    Understood! Super amazing explanation as always, thank you very very very much for your effort!!

  • @AbhishekShukla-k8f
    @AbhishekShukla-k8f 23 дня назад

    I really enjoyed your explanation of the count inversions problem. It was very clear and easy to understand.

  • @shiveshgupta1705
    @shiveshgupta1705 Год назад +5

    i just imagine if all the problems would be available on this channel in future

  • @prateek5208
    @prateek5208 6 месяцев назад +2

    such an awesome expalination bhaiya just approach dekhkr hi dimag ma intution agya ki merge ma kase implement krenge , you are best

  • @ErenYeager-dp4er
    @ErenYeager-dp4er 19 дней назад +4

    Done on 9 Jan 2025 at 11:54
    Place : Study Room 2 , Hostel 5 , IIT Bombay

    • @user93-i2k
      @user93-i2k 15 дней назад

      btech or mtech?
      which year?

  • @PriyaGanti-m7c
    @PriyaGanti-m7c 4 месяца назад +2

    I understood the solution clearly... Thank you very much, sir.

  • @aditijagtap7853
    @aditijagtap7853 6 месяцев назад +1

    Best teaching approach so far!!!!

  • @bgmihighlights6378
    @bgmihighlights6378 2 месяца назад +1

    Omg Striver you are great , someday will love to reach at your level!

  • @selvaarumugam370
    @selvaarumugam370 Год назад +4

    As usual your teaching jus made coding much easier than it is bruh!! Waiting for Binary Search series bruh!!!!

  • @InduAnuga
    @InduAnuga 8 месяцев назад +3

    Understood
    THE BEST EXPLANATION
    Excellent playlist 👌 👏 ❤

  • @AnushreeSinha-ec3jt
    @AnushreeSinha-ec3jt 2 дня назад

    Amazing explanation Striver!! ✨

  • @prateekgoyal7009
    @prateekgoyal7009 Год назад +2

    This is the best explanation I've ever got.

  • @xtzyrox2764
    @xtzyrox2764 Год назад +17

    I am happy with the brute force now I will see optimal 1 week before interview bcz University exams are in this month

  • @SaumyaSharma007
    @SaumyaSharma007 Год назад +2

    OMG Bawal explanation Striver Bhaiya.😃

  • @mehulthuletiya497
    @mehulthuletiya497 Год назад +26

    Timestamps:
    ---------------------
    00:40 Problem Statement
    02:11 Brute force approach
    02:52 Pseudocode
    03:35 Complexity
    04:05 Optimmal solution
    04:22 Intuition
    10:33 Approach + Dry-run
    18:02 Code
    22:37 Complexity

  • @pallavi22222
    @pallavi22222 Год назад +2

    your problem solving approach explanation is superb

  • @lakshaysawhney9988
    @lakshaysawhney9988 6 месяцев назад +1

    Thoroughly enjoyed the problem!!

  • @sourabhsoni5065
    @sourabhsoni5065 Месяц назад

    Just cleared the intution so well.

  • @JatinSaini-g4y
    @JatinSaini-g4y 12 дней назад

    tum bahut mast kaam karta hai maksood bhai...🫂

  • @pranavdharkar
    @pranavdharkar 27 дней назад

    I feel so happy when i saw that the merge function returns the cnt and then add it to the ans i had exactly done the same

  • @aradhyaagrawal4766
    @aradhyaagrawal4766 4 месяца назад +1

    00:06 Count the number of pairs in an array where the left element is greater than the right element.
    02:12 Brute Force solution
    04:10 Counting the number of pairs in two sorted arrays where the left element is from the left array and the right element is from the right array, and the right element is smaller than the left element.
    06:11 Inversion count can be found by counting the number of elements greater than the element on its right in a sorted array
    08:28 The number of pairs formed in an array can be determined using the merge sort algorithm.
    10:41 Counting inversions in an array by merging sorted subarrays.
    13:03 Count the inversions in an array using a merge sort algorithm
    15:17 Merge sort algorithm helps in counting inversions in an array
    17:25 Count the number of inversions in an array
    19:15 Count the number of inversions in an array
    21:08 The solution uses a recursive function to merge and count inversions in an array.
    22:51 The time complexity of counting inversions is O(n log n) and it requires extra space to merge arrays.

  • @chiragbirla5606
    @chiragbirla5606 Год назад +2

    Best explanation ever for this problem

  • @vikasbagri1225
    @vikasbagri1225 Год назад +4

    Understood it very well
    Thanks for this amazing series

  • @akritisneh2849
    @akritisneh2849 Год назад +5

    So much crystal clear!!!!Thank youu❤

  • @abhaypatel379
    @abhaypatel379 7 месяцев назад

    thanks striver for making a complex question into very easy question
    🤗

  • @the_haryannvi_coder
    @the_haryannvi_coder 8 месяцев назад +2

    If u don't wanna use cnt in mergeSort function, you can do this:-
    int mergeSort(vector &arr, int low, int high) {
    if (low >= high) return 0;

    int mid = (low + high) / 2 ;
    int left = mergeSort(arr, low, mid); // left half
    int right = mergeSort(arr, mid + 1, high); // right half
    int m = merge(arr, low, mid, high); // merging sorted halves
    return left + right + m;
    }

  • @NoniSabharwal
    @NoniSabharwal 9 месяцев назад

    The way he said wow! Uff in love with the voice

  • @HR-pz7ts
    @HR-pz7ts 9 месяцев назад

    Amazing I solved two questions using the same logic.

  • @vishakhakhanna8115
    @vishakhakhanna8115 3 месяца назад

    Understood! Great explaination.

  • @elamaran350
    @elamaran350 4 месяца назад +1

    Understood ❤ At last

  • @iamnoob7593
    @iamnoob7593 Месяц назад

    Brilliant video. Thank you striver

  • @shivansh.speaks
    @shivansh.speaks Год назад

    Really it was a great series Striver.🔥🔥

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

    UNDERSTOOD SIR ! GREAT EXPLAINATION

  • @8bit_hero850
    @8bit_hero850 10 месяцев назад +19

    Can we realistically solve this if it comes in an interview given we haven't solved it before? I mean how do can you get the intuition of merge sort from this problem? I really don't get it.

    • @unknown47896
      @unknown47896 6 месяцев назад +1

      no way bro

    • @flakky626
      @flakky626 5 месяцев назад +1

      Exactly my thought, No way you gonnna come up with something like that on your own

  • @rushidesai2836
    @rushidesai2836 Месяц назад

    Crazy good solution.

  • @prabhakaran5542
    @prabhakaran5542 4 месяца назад +1

    Understood ❤

  • @GnaniDarshan
    @GnaniDarshan Месяц назад

    Nice intution!
    Thanks

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

    Best explanation ever

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

    good explanation on count inversion

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

    loved the optimal solution, intuition op!

  • @shashwatpandey3285
    @shashwatpandey3285 3 месяца назад +2

    took me 1 day to solve this problem on my own , no outside help or anything , when i first saw the problem it was like hmm , we want total number of elements less than arr[i] on its right side , so it was basically like the array was being spilt, at first i coded the bruteforce which was very easy ofcoursse , and then i had to brainstorm for hours , in the question it was said the inversion count tells us how far or close the array is from being sorted , thats when merge sort came into my mind , because as i said earlier we want number of elements less than arr[i] on the right side of i , merge sort was the only soritng algo i studied in which arrays were being split and i thought while merging 2 sorted arrays we can count the number of elements smaller than arr[i[, from the right side of the splitted array

  • @gauristar4094
    @gauristar4094 6 месяцев назад

    Superb logic, Understood!!!

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

    // Everytime while sorting you move an element to the left (assume nobody moves to right agar chote walo ko aana hoga to left me aa jayenge)
    // if an element crosses another element while moving to the left for the purpose of sorting then it should increase the count of inversion

  • @KapilMaan-vw9sd
    @KapilMaan-vw9sd 4 месяца назад

    thanks sir for making this wonderful video sir !!!

  • @pavanjegurupati
    @pavanjegurupati 10 месяцев назад

    Great explanation!!

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

    just wow explanation

  • @kaichang8186
    @kaichang8186 4 месяца назад

    Thanks for the great video, well understood

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

    All i can say is Thankyou so much ❤🙌

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

    awesome as always!!!!🤩

  • @harigs72
    @harigs72 4 месяца назад +1

    Happy teacher day 🎉❤

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

    13:05 the kind of excitement I want while learning DSA.

  • @shivanshagrawal4179
    @shivanshagrawal4179 2 месяца назад

    understood the solution striver thankyou

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 2 месяца назад

    NICE SUPER EXCELLENT MOTIVATED

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

    understood!
    def merge(arr,l,mid,h):
    temp=[]
    i=l
    j=mid+1
    cnt=0
    while i

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

    Understood amazing explanation

  • @anshasati1920
    @anshasati1920 7 месяцев назад

    Understood Sir🥳

  • @SukanyaGhosh-c4t
    @SukanyaGhosh-c4t Год назад +1

    Thank You Bhaiya

  • @steverogers7050
    @steverogers7050 10 месяцев назад

    very nice explanation bhaiya

  • @muqeemuddin8057
    @muqeemuddin8057 11 месяцев назад +1

    Hey, can you make a video on binary insertion sort and compare it's time complexities with insertion sort. Thanks for your videos on DSA.

  • @harim7945
    @harim7945 Месяц назад

    Striver is my man crush now lol after Tom cruise....jk You are great man....your videos help me a lot!

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

    Thank you for this video !!

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

    Badhiya kaam kar rahe ho, see you soon

  • @MJBZG
    @MJBZG 7 месяцев назад +1

    didn't understand much but will try again

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

    Understood✅🔥🔥

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

    Superb Explanation

  • @Malayalam_learner
    @Malayalam_learner 23 часа назад

    Antha bavundi Bammardi
    Unna array ni kelakkunda
    I mean sort cheyyakunda count inversion cheyyalema?
    Merge sort ante sorting which is manipulation of array😅😅
    Okok ardamindi just ippudu realised bammardi thanks ❤

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

    understood the approach sir
    thanks alot

  • @ARPITAAGARWAL-k2k
    @ARPITAAGARWAL-k2k 5 дней назад

    // Function to count inversions in the array.
    int merge(vector &arr, int low, int mid, int high){
    int cnt=0;
    int left=low;
    int right=mid+1;
    vector temp;
    while(left

  • @hashcodez757
    @hashcodez757 10 месяцев назад

    understood Bhaiya!!

  • @the_avii_7
    @the_avii_7 Месяц назад

    Please take care, in this problem inside 1st while loop if-condition should be { lesser than equal to ---> if ( arr[left]

  • @NazeerBashaShaik
    @NazeerBashaShaik 10 месяцев назад

    Understood, thank you.

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 10 месяцев назад

    Nice lecture................

  • @vk-mc5tq
    @vk-mc5tq 10 месяцев назад

    Global vs Local
    In Global variables values updated dynamically but in local variables we need to pass updated values (manually) to subsequent functions

  • @GhostVaibhav
    @GhostVaibhav 11 месяцев назад

    Understood🔥

  • @abhishekverma4693
    @abhishekverma4693 11 месяцев назад

    thank you so much for watching

  • @JatinKumar-pl4sq
    @JatinKumar-pl4sq 4 месяца назад

    Bro u have destroyed all so called tech schools

  • @grm642
    @grm642 11 месяцев назад

    Thank you😊

  • @parasarora5869
    @parasarora5869 Месяц назад

    Epic 🔥

  • @ShubhamKumar-uf3gc
    @ShubhamKumar-uf3gc 7 месяцев назад

    loved that bhaiya

  • @gowtham9153
    @gowtham9153 Час назад

    13:05 - The OG Striver

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

    Understood brother❤️

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

    Understood 🎉

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

    Great video

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

    I understood the problem

  • @gautamsaxena4647
    @gautamsaxena4647 3 месяца назад

    understood bhaiya

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

    thanks alot bhaiya

  • @LalitSingh-yn6zw
    @LalitSingh-yn6zw 3 месяца назад

    13:09 WOW!!!