Counting inversions in an array
Vložit
- čas přidán 1. 09. 2019
- 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 :)
I always thought it was an extremely hard problem. This explanation made it look so easy. Thanks.
Welcome :)
Best explanation I have seen so far
Thanks :)
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 😊
Welcome :)
One best thing is that
U always take tough example which explain all the possible situation....
Awesome explanation..
Thanks sir...
Welcome :)
Clear and crisp explanation. Thankyou for creating such a good stuff.
Welcome :)
this is called explanation with good knowledge
Very clear explained! Thanks. That's what I needed!
Welcome :)
The best explanation for this problem 👏 👌
you are really tech dose....ur explanation is very good.
Crystal clear explanation. Thanks a lot!!
Welcome :)
Picture explains everything, thanks.
very clear explanation thank you
After seeing this video this problem became a very easy problem. Thank you
Welcome :)
Best Channel Ever! You are doing good work. :)
Thanks :)
This was an amazing explanation! Thank you!
Welcome :)
Helped a lot!
Thank you!
Welcome :)
The Best explanation ever... 🙏🙏🙏
Thanks
bhai 1 number....I always prefer your video when I get stuck on any problem.
Nice :)
Clear explanation dude
Precise and too the point :)
Thanks
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!!!!!!!!!!!!!
Nice to hear this :)
Thanks man, explaining step by step is the clearest method. Now I got this algorithm.
Nice 😃
thankk you bhaiya!! amazing
Crystal clear explanation..
Thanks :)
You are genius :-) Thank You
😅 Welcome
Wow what a great explanation 👍
Thanks
really helped a lot
Nice explanation sir....you are doing a great job
Thanks :)
Great explanation!
Thanks
very nice explaination ever seen .thanks a lot
Welcome
Thanks for the explanation
Welcome
Awesome 👍
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)
thank you so much
Excellent Explanation
Thanks
In the 3rd method, the std::distance time complexity on std::multiset is O(n), so the overall time complexity is O(n^2)
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.
Hey Man!
How to access the indexOf an elem in TreeSet ..?
@@aeroabrar_31 treeSet.headSet().size
very very good explanation
Thanks :)
nice video ,really helpful
Thanks :)
THANK YOU SO MUUUUUUUUCH
Atlast understood ❤❤
Great :)
Nice explaination, BIT method too (:
Okay
best explanation
Glad you think so!
Superb
Thanks for this my book didnt show this well
😁
great explanation
Thanks
merge sort technique is the best for this problem
merge sort is very understandi all us
thnx a lot
Awesome Sir!
Sir please make a video on BIT also
Yea...will make it.
Thank You for the explanation. Where can I find Method - 5 explanation?
Man I got understood everything, I saw there
Nice :)
RIP english
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!
@techdose can you explain the complexity of the 3rd method?
Ya exactly I have the same doubt ..
He used distance function which take O(n) time
Could you please provide its code. I am not able to find it anywhere
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.
انت راقي احبك😘
😅
Last method was 💖
😂 Using multiset is the easiest :P
Sir in BiT updating value and getting prefix sum takes O(log(n)) time then how it is supposed to be O(n)
It would be very good if you shown code also,by the way it is fabulous.
Yea I am doing it from March 2019.
a[i]>a[j] and j>i , this is the inversion
i think your inversion is wrng
time complexity for BIT method is O(nlogn)
The multiset method doesn't satisfy the time constraint as the distance method takes O(n) for multisets.
Please make a video on AVL tree and BIT. It would be very helpful.
It will come later when I resume Segment tree and TREE.
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
I think the MultiSet implementation is O(nlogn)
upper_bound => O(logn) in multiset
We do upper_bound 'n' times
Therefore, O(nlogn)
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
N² bcz a loop is outside for insertion O(n) and distance stl gonna take O(n) for calucating distance.
Hence N²
@@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)
Why do we need to merge the partitions?
So it's like checking if they are sorted or not?
method 2-merge sort 4:30
method 3-multiset 14:30
Awesome
Thanks
we can use stack also
Thanks
Welcome
Is the second method based on the merge sort called Piggybacking on merge sort also?
pls discuss the BIT method
Yes sure
I am very curious you make very good videos..what do you do full time?
Software Developement Engineer.
Multiset😍
😅
can you make a video for the inversion count using BIT??
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.
Please explain BIT
Which software do you use for the anntoations?
Ink2go :)
@@techdose4u thanks! may I ask which pad do you use to do the actual writing?
Can you please tell me the Space Complexity of the 2nd method??
space complexity is actually O(n) like merge sort since you need extra array to store the numbers in sorted order
getting TLE with set
could you give the code for method 2?
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
Sir can u pls provide the explanation for Binary indexed method for the same problem
Sure.... When I get time I will
@@techdose4u thanks sir
Hi please explain BIT method
Sure.
can you explain the BIT method
Yea....I will cover this problem when I cover BIT
Sir I can't apply Fenwick tree properly. Please apply Fenwick tree to solve this problem .
I will do it after DP.
From where I get the code?
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.
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
Sir, can you please provide the code for all the methods and explain all the methods in another video. Please sir.
CODE LINK: www.geeksforgeeks.org/counting-inversions/
I will explain the methods in detail later.
@@techdose4u You are doing a great job sir. Could you please share your FB Id so that i can connect.
why is this solution not working for gfg and leetcode problems?
I think in the method 3 ,the time complexity should be o(nlogn).
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)
sir please also provide a code of this problem
Please try to code yourself
Ok sir
difference kaise nikale
3rd method should be O(nlogn) right? ( we dont need to use "distance")
:)
multiset me minus nhi hota h sir
Sir can you share the source code
Please try to search in comment section
I know the solution but not the intuition that leads to merge sort.
you should change your name to Placement Cracker
Let it be techdose :P
@@techdose4u Thank you So much ,Sir you are a great help .
At 3:10 you accidentally said 4 is greater than 6 hence there is inversion.
Got confused for a bit 😛
:P
Great explanation!
Could you also make a video about how to get the number of inversions per element in the original array?
Sure.....but can you suggest me a proper title for that. That will be helpful to see if users need it or not.
How about "count of smaller numbers after self" ?
I guess I should have explained that in video itself 😅
maal
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
best explanation
Thanks :)