Counting inversions in an array
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- 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 :)
One best thing is that
U always take tough example which explain all the possible situation....
Awesome explanation..
Thanks sir...
Welcome :)
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 :)
After seeing this video this problem became a very easy problem. Thank you
Welcome :)
Clear and crisp explanation. Thankyou for creating such a good stuff.
Welcome :)
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)
bhai 1 number....I always prefer your video when I get stuck on any problem.
Nice :)
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 :)
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
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
Crystal clear explanation..
Thanks :)
this is called explanation with good knowledge
time complexity for BIT method is O(nlogn)
Picture explains everything, thanks.
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.
Thank You for the explanation. Where can I find Method - 5 explanation?
The Best explanation ever... 🙏🙏🙏
Thanks
Very clear explained! Thanks. That's what I needed!
Welcome :)
you are really tech dose....ur explanation is very good.
Crystal clear explanation. Thanks a lot!!
Welcome :)
You are genius :-) Thank You
😅 Welcome
The best explanation for this problem 👏 👌
The multiset method doesn't satisfy the time constraint as the distance method takes O(n) for multisets.
Best Channel Ever! You are doing good work. :)
Thanks :)
very very good explanation
Thanks :)
very nice explaination ever seen .thanks a lot
Welcome
very clear explanation thank you
Clear explanation dude
This was an amazing explanation! Thank you!
Welcome :)
Nice explanation sir....you are doing a great job
Thanks :)
Thanks man, explaining step by step is the clearest method. Now I got this algorithm.
Nice 😃
Man I got understood everything, I saw there
Nice :)
RIP english
Great explanation!
Thanks
I am very curious you make very good videos..what do you do full time?
Software Developement Engineer.
Awesome Sir!
Sir please make a video on BIT also
Yea...will make it.
thankk you bhaiya!! amazing
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)
Excellent Explanation
Thanks
Thanks for the explanation
Welcome
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 in BiT updating value and getting prefix sum takes O(log(n)) time then how it is supposed to be O(n)
Precise and too the point :)
Thanks
Why do we need to merge the partitions?
merge sort is very understandi all us
Wow what a great explanation 👍
Thanks
pls discuss the BIT method
Yes sure
THANK YOU SO MUUUUUUUUCH
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
best explanation
Glad you think so!
It would be very good if you shown code also,by the way it is fabulous.
Yea I am doing it from March 2019.
great explanation
Thanks
Superb
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.
merge sort technique is the best for this problem
Thanks for this my book didnt show this well
😁
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.
Helped a lot!
Thank you!
Welcome :)
thank you so much
Atlast understood ❤❤
Great :)
nice video ,really helpful
Thanks :)
a[i]>a[j] and j>i , this is the inversion
i think your inversion is wrng
we can use stack also
method 2-merge sort 4:30
method 3-multiset 14:30
Awesome 👍
Can you please explain this question in single file programming.
So it's like checking if they are sorted or not?
Sir I can't apply Fenwick tree properly. Please apply Fenwick tree to solve this problem .
I will do it after DP.
really helped a lot
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
thnx a lot
Multiset😍
😅
Is the second method based on the merge sort called Piggybacking on merge sort also?
Awesome
Thanks
Nice explaination, BIT method too (:
Okay
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
Last method was 💖
😂 Using multiset is the easiest :P
Hi please explain BIT method
Sure.
Thanks
Welcome
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
why is this solution not working for gfg and leetcode problems?
انت راقي احبك😘
😅
can you explain the BIT method
Yea....I will cover this problem when I cover 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?
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.
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.
getting TLE with set
multiset me minus nhi hota h sir
I know the solution but not the intuition that leads to merge sort.
3rd method should be O(nlogn) right? ( we dont need to use "distance")
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)
Please explain BIT
sir please also provide a code of this problem
Please try to code yourself
Ok sir
difference kaise nikale
Sir can you share the source code
Please try to search in comment section
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 😅
:)
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
maal