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 ❤
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
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.
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; }
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.
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
// 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
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 ❤
// 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
Global vs Local In Global variables values updated dynamically but in local variables we need to pass updated values (manually) to subsequent functions
Please watch our new video on the same topic: ruclips.net/video/AseUmwVNaoY/видео.html
this link take u to the same video we are watching
@@namannema3349 thats what recursion means
@@yogeshkaushik8316 😆
Best count inversion video on RUclips, your method of teaching is very best that it gets me understand very easily 🌟🌟🌟
Nowadaya strivers voice has become so calm and soft 😅❤
True...as compared to his previous vdos 😂
😂
@@anuradha3868 same thought 😂
abe mic kharap tha pehle😂
ladki ka chakar
Making the complex problem looks simpler is a kind of skill that striver only has🛐
this is the best playlist in world. thank you stiver for your effort.
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 ❤
whats the complexity of your solution?
can you share it?
@@bishakhdutta8427 same as merger sort merge algorithm
Completed the playlist. This was the best recursion playlist I ever went through. Thanks a lot Striver.
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
Yet another great video Striver. Thank You!! I'll definetly support this channel from my first salary if I get placed.
Striver your voice is very soothing and calm bro ❤.... I use it to sleep at night AND study... and I don't feel sleepy
Bro what
@@mranonymous1982 lol
The best count inversion video on RUclips.. Thanks a lot Raj.. stay blessed❤
I was struggling with the solution but as soon as you mentioned merge sort it clicked in my mind
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:>)
Understood! Super amazing explanation as always, thank you very very very much for your effort!!
I really enjoyed your explanation of the count inversions problem. It was very clear and easy to understand.
i just imagine if all the problems would be available on this channel in future
such an awesome expalination bhaiya just approach dekhkr hi dimag ma intution agya ki merge ma kase implement krenge , you are best
Done on 9 Jan 2025 at 11:54
Place : Study Room 2 , Hostel 5 , IIT Bombay
btech or mtech?
which year?
I understood the solution clearly... Thank you very much, sir.
Best teaching approach so far!!!!
Omg Striver you are great , someday will love to reach at your level!
As usual your teaching jus made coding much easier than it is bruh!! Waiting for Binary Search series bruh!!!!
Understood
THE BEST EXPLANATION
Excellent playlist 👌 👏 ❤
Amazing explanation Striver!! ✨
This is the best explanation I've ever got.
I am happy with the brute force now I will see optimal 1 week before interview bcz University exams are in this month
Did anyone ask you?
OMG Bawal explanation Striver Bhaiya.😃
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
your problem solving approach explanation is superb
Thoroughly enjoyed the problem!!
Just cleared the intution so well.
tum bahut mast kaam karta hai maksood bhai...🫂
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
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.
Best explanation ever for this problem
Understood it very well
Thanks for this amazing series
So much crystal clear!!!!Thank youu❤
thanks striver for making a complex question into very easy question
🤗
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;
}
The way he said wow! Uff in love with the voice
Amazing I solved two questions using the same logic.
Understood! Great explaination.
Understood ❤ At last
Brilliant video. Thank you striver
Really it was a great series Striver.🔥🔥
UNDERSTOOD SIR ! GREAT EXPLAINATION
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.
no way bro
Exactly my thought, No way you gonnna come up with something like that on your own
Crazy good solution.
Understood ❤
Nice intution!
Thanks
Best explanation ever
good explanation on count inversion
loved the optimal solution, intuition op!
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
goodforyou man
Superb logic, Understood!!!
// 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
thanks sir for making this wonderful video sir !!!
Great explanation!!
just wow explanation
Thanks for the great video, well understood
All i can say is Thankyou so much ❤🙌
awesome as always!!!!🤩
Happy teacher day 🎉❤
13:05 the kind of excitement I want while learning DSA.
understood the solution striver thankyou
NICE SUPER EXCELLENT MOTIVATED
understood!
def merge(arr,l,mid,h):
temp=[]
i=l
j=mid+1
cnt=0
while i
Understood amazing explanation
Understood Sir🥳
Thank You Bhaiya
very nice explanation bhaiya
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.
Striver is my man crush now lol after Tom cruise....jk You are great man....your videos help me a lot!
Thank you for this video !!
Badhiya kaam kar rahe ho, see you soon
didn't understand much but will try again
Understood✅🔥🔥
Superb Explanation
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 ❤
understood the approach sir
thanks alot
// 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
understood Bhaiya!!
Please take care, in this problem inside 1st while loop if-condition should be { lesser than equal to ---> if ( arr[left]
Understood, thank you.
Nice lecture................
Global vs Local
In Global variables values updated dynamically but in local variables we need to pass updated values (manually) to subsequent functions
Understood🔥
thank you so much for watching
Bro u have destroyed all so called tech schools
Thank you😊
Epic 🔥
loved that bhaiya
13:05 - The OG Striver
Understood brother❤️
Understood 🎉
Great video
I understood the problem
understood bhaiya
thanks alot bhaiya
13:09 WOW!!!