Counting inversions in an array

  Рет қаралды 91,021

Techdose

Techdose

5 жыл бұрын

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 :)

Пікірлер: 184
@AarshSharma
@AarshSharma 4 жыл бұрын
I always thought it was an extremely hard problem. This explanation made it look so easy. Thanks.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@ShivamSingh-me1nb
@ShivamSingh-me1nb 4 жыл бұрын
Best explanation I have seen so far
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@tanayvartak4362
@tanayvartak4362 3 жыл бұрын
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 😊
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@VikasKumar-nb2pn
@VikasKumar-nb2pn 4 жыл бұрын
One best thing is that U always take tough example which explain all the possible situation.... Awesome explanation.. Thanks sir...
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@SinghFlex
@SinghFlex 4 жыл бұрын
Clear and crisp explanation. Thankyou for creating such a good stuff.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@jarno9340
@jarno9340 3 жыл бұрын
Very clear explained! Thanks. That's what I needed!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@AyushJain-ot9yv
@AyushJain-ot9yv 4 жыл бұрын
Best Channel Ever! You are doing good work. :)
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sagargupta1615
@sagargupta1615 4 жыл бұрын
you are really tech dose....ur explanation is very good.
@deepakprajapati5064
@deepakprajapati5064 3 жыл бұрын
this is called explanation with good knowledge
@kaushikramabhotla4635
@kaushikramabhotla4635 2 жыл бұрын
The best explanation for this problem 👏 👌
@praveenchouhan6388
@praveenchouhan6388 3 жыл бұрын
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!!!!!!!!!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Nice to hear this :)
@sukritkapil2562
@sukritkapil2562 4 жыл бұрын
Crystal clear explanation. Thanks a lot!!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@RaGa_BABA
@RaGa_BABA 3 жыл бұрын
bhai 1 number....I always prefer your video when I get stuck on any problem.
@techdose4u
@techdose4u 3 жыл бұрын
Nice :)
@shubhammishra3783
@shubhammishra3783 4 жыл бұрын
very clear explanation thank you
@b9944236
@b9944236 7 ай бұрын
Picture explains everything, thanks.
@mansiagrawal4039
@mansiagrawal4039 3 жыл бұрын
After seeing this video this problem became a very easy problem. Thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@vasujain1970
@vasujain1970 3 жыл бұрын
This was an amazing explanation! Thank you!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@PizzaGamingTheReal
@PizzaGamingTheReal 3 жыл бұрын
Thanks man, explaining step by step is the clearest method. Now I got this algorithm.
@techdose4u
@techdose4u 3 жыл бұрын
Nice 😃
@patilsoham
@patilsoham 4 жыл бұрын
Helped a lot! Thank you!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@sunwado
@sunwado 3 жыл бұрын
Precise and too the point :)
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@sainathsingineedi2922
@sainathsingineedi2922 3 жыл бұрын
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)
@javadsayevand8273
@javadsayevand8273 3 жыл бұрын
The Best explanation ever... 🙏🙏🙏
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@changjeffreysinto3872
@changjeffreysinto3872 2 жыл бұрын
Clear explanation dude
@divyareddy7622
@divyareddy7622 Жыл бұрын
thankk you bhaiya!! amazing
@sharuk3545
@sharuk3545 3 жыл бұрын
Awesome 👍
@Navneetkumar-os5cl
@Navneetkumar-os5cl 3 жыл бұрын
Sir in BiT updating value and getting prefix sum takes O(log(n)) time then how it is supposed to be O(n)
@jpakash1999
@jpakash1999 4 жыл бұрын
You are genius :-) Thank You
@techdose4u
@techdose4u 4 жыл бұрын
😅 Welcome
@elizabethr5161
@elizabethr5161 Жыл бұрын
Crystal clear explanation..
@techdose4u
@techdose4u Жыл бұрын
Thanks :)
@shobhitbajaj9667
@shobhitbajaj9667 4 жыл бұрын
Wow what a great explanation 👍
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@paragroy5359
@paragroy5359 3 жыл бұрын
Nice explanation sir....you are doing a great job
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@Joyddep
@Joyddep 3 жыл бұрын
Great explanation!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@ayushkawatra7436
@ayushkawatra7436 3 жыл бұрын
really helped a lot
@vikashkumarchaurasia1299
@vikashkumarchaurasia1299 4 жыл бұрын
very nice explaination ever seen .thanks a lot
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@kishalaybhattacharya8312
@kishalaybhattacharya8312 3 жыл бұрын
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
@amit36666
@amit36666 4 жыл бұрын
In the 3rd method, the std::distance time complexity on std::multiset is O(n), so the overall time complexity is O(n^2)
@abhisheks8017
@abhisheks8017 3 жыл бұрын
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.
@aeroabrar_31
@aeroabrar_31 Жыл бұрын
Hey Man! How to access the indexOf an elem in TreeSet ..?
@TJVargasS
@TJVargasS Жыл бұрын
@@aeroabrar_31 treeSet.headSet().size
@priyasharma3549
@priyasharma3549 2 жыл бұрын
thank you so much
@todo1553
@todo1553 2 жыл бұрын
Thanks for the explanation
@techdose4u
@techdose4u 2 жыл бұрын
Welcome
@kollivenkatamadhukar5059
@kollivenkatamadhukar5059 4 жыл бұрын
Excellent Explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@tusharkumar3277
@tusharkumar3277 4 жыл бұрын
nice video ,really helpful
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@22.jananidileepan44
@22.jananidileepan44 Жыл бұрын
Superb
@KathanShah03
@KathanShah03 2 жыл бұрын
Thank You for the explanation. Where can I find Method - 5 explanation?
@harshitagrawal789
@harshitagrawal789 4 жыл бұрын
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!
@kunalpahwa7720
@kunalpahwa7720 4 жыл бұрын
@techdose can you explain the complexity of the 3rd method?
@softwaredevelopment4392
@softwaredevelopment4392 4 жыл бұрын
Ya exactly I have the same doubt ..
@adelikomtyagi5324
@adelikomtyagi5324 4 жыл бұрын
He used distance function which take O(n) time
@kanishkagupta5801
@kanishkagupta5801 3 жыл бұрын
Could you please provide its code. I am not able to find it anywhere
@vipulkrishna19
@vipulkrishna19 4 жыл бұрын
very very good explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@jakubfraczek1208
@jakubfraczek1208 Жыл бұрын
THANK YOU SO MUUUUUUUUCH
@ShreyaSingh-vr9qi
@ShreyaSingh-vr9qi 4 жыл бұрын
Nice explaination, BIT method too (:
@techdose4u
@techdose4u 4 жыл бұрын
Okay
@SudhanshuKumar-jl8iw
@SudhanshuKumar-jl8iw 3 жыл бұрын
great explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@theSilentPsycho
@theSilentPsycho 4 жыл бұрын
best explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@diptamo
@diptamo Жыл бұрын
Atlast understood ❤❤
@techdose4u
@techdose4u Жыл бұрын
Great :)
@anjana1700
@anjana1700 4 жыл бұрын
Awesome Sir! Sir please make a video on BIT also
@techdose4u
@techdose4u 4 жыл бұрын
Yea...will make it.
@_PAYALGAIKWAD
@_PAYALGAIKWAD 2 жыл бұрын
thnx a lot
@excessreactant9045
@excessreactant9045 4 жыл бұрын
Thanks for this my book didnt show this well
@techdose4u
@techdose4u 4 жыл бұрын
😁
@shubhamprashar7676
@shubhamprashar7676 3 жыл бұрын
Why do we need to merge the partitions?
@yyndsai
@yyndsai 3 жыл бұрын
So it's like checking if they are sorted or not?
@hadeerzayat355
@hadeerzayat355 3 жыл бұрын
Is the second method based on the merge sort called Piggybacking on merge sort also?
@biswamohandwari6460
@biswamohandwari6460 4 жыл бұрын
Man I got understood everything, I saw there
@techdose4u
@techdose4u 4 жыл бұрын
Nice :)
@ranjanpanda2586
@ranjanpanda2586 4 жыл бұрын
RIP english
@anitasingh-jw1lc
@anitasingh-jw1lc Жыл бұрын
merge sort is very understandi all us
@dayanandraut5660
@dayanandraut5660 3 жыл бұрын
merge sort technique is the best for this problem
@yuktykhandelia5312
@yuktykhandelia5312 3 жыл бұрын
The multiset method doesn't satisfy the time constraint as the distance method takes O(n) for multisets.
@lifecodesher5818
@lifecodesher5818 3 жыл бұрын
I am very curious you make very good videos..what do you do full time?
@techdose4u
@techdose4u 3 жыл бұрын
Software Developement Engineer.
@AliObaid1992
@AliObaid1992 4 жыл бұрын
انت راقي احبك😘
@techdose4u
@techdose4u 4 жыл бұрын
😅
@mukulrustagi9404
@mukulrustagi9404 3 жыл бұрын
Awesome
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@TomerBenDavid
@TomerBenDavid 4 жыл бұрын
Which software do you use for the anntoations?
@techdose4u
@techdose4u 4 жыл бұрын
Ink2go :)
@TomerBenDavid
@TomerBenDavid 4 жыл бұрын
@@techdose4u thanks! may I ask which pad do you use to do the actual writing?
@Samir-ll3kj
@Samir-ll3kj 3 жыл бұрын
Please make a video on AVL tree and BIT. It would be very helpful.
@techdose4u
@techdose4u 3 жыл бұрын
It will come later when I resume Segment tree and TREE.
@UnKnown-id7ih
@UnKnown-id7ih 3 жыл бұрын
It would be very good if you shown code also,by the way it is fabulous.
@techdose4u
@techdose4u 3 жыл бұрын
Yea I am doing it from March 2019.
@kashishsharma6809
@kashishsharma6809 2 жыл бұрын
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.
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Last method was 💖
@techdose4u
@techdose4u 4 жыл бұрын
😂 Using multiset is the easiest :P
@ankitsinghgautam1274
@ankitsinghgautam1274 4 жыл бұрын
Thanks
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@vivekshaw2437
@vivekshaw2437 4 жыл бұрын
can you make a video for the inversion count using BIT??
@techdose4u
@techdose4u 4 жыл бұрын
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.
@hitaishidas4955
@hitaishidas4955 4 жыл бұрын
Can you please tell me the Space Complexity of the 2nd method??
@prateeksinghal630
@prateeksinghal630 4 жыл бұрын
space complexity is actually O(n) like merge sort since you need extra array to store the numbers in sorted order
@satyamgupta5234
@satyamgupta5234 2 жыл бұрын
we can use stack also
@manavmalhotra8513
@manavmalhotra8513 3 жыл бұрын
a[i]>a[j] and j>i , this is the inversion
@manavmalhotra8513
@manavmalhotra8513 3 жыл бұрын
i think your inversion is wrng
@bhuvaneshwaranr5566
@bhuvaneshwaranr5566 4 жыл бұрын
could you give the code for method 2?
@nemaligirisai2896
@nemaligirisai2896 3 жыл бұрын
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
@Mandeepsingh-jo5cf
@Mandeepsingh-jo5cf Жыл бұрын
time complexity for BIT method is O(nlogn)
@gokulnaathb2627
@gokulnaathb2627 2 жыл бұрын
Multiset😍
@techdose4u
@techdose4u 2 жыл бұрын
😅
@siddhantjaiswal4231
@siddhantjaiswal4231 4 жыл бұрын
pls discuss the BIT method
@techdose4u
@techdose4u 4 жыл бұрын
Yes sure
@karanshah838
@karanshah838 3 жыл бұрын
I think the MultiSet implementation is O(nlogn) upper_bound => O(logn) in multiset We do upper_bound 'n' times Therefore, O(nlogn)
@vetald1979
@vetald1979 3 жыл бұрын
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
@aesthetic_vibes_404
@aesthetic_vibes_404 3 жыл бұрын
N² bcz a loop is outside for insertion O(n) and distance stl gonna take O(n) for calucating distance. Hence N²
@karanshah838
@karanshah838 3 жыл бұрын
@@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)
@MohitKumar-rq8hv
@MohitKumar-rq8hv 2 жыл бұрын
method 2-merge sort 4:30 method 3-multiset 14:30
@kislayapant6119
@kislayapant6119 4 жыл бұрын
Sir can u pls provide the explanation for Binary indexed method for the same problem
@techdose4u
@techdose4u 4 жыл бұрын
Sure.... When I get time I will
@kislayapant6119
@kislayapant6119 4 жыл бұрын
@@techdose4u thanks sir
@anjalipatel5643
@anjalipatel5643 24 күн бұрын
Can you please explain this question in single file programming.
@sutirthasadhukhan78
@sutirthasadhukhan78 3 жыл бұрын
Sir I can't apply Fenwick tree properly. Please apply Fenwick tree to solve this problem .
@techdose4u
@techdose4u 3 жыл бұрын
I will do it after DP.
@softwaredevelopment4392
@softwaredevelopment4392 4 жыл бұрын
Please explain BIT
@dayanandraut5660
@dayanandraut5660 2 жыл бұрын
why is this solution not working for gfg and leetcode problems?
@sayantangupta1403
@sayantangupta1403 4 жыл бұрын
Sir, can you please provide the code for all the methods and explain all the methods in another video. Please sir.
@techdose4u
@techdose4u 4 жыл бұрын
CODE LINK: www.geeksforgeeks.org/counting-inversions/
@techdose4u
@techdose4u 4 жыл бұрын
I will explain the methods in detail later.
@sayantangupta1403
@sayantangupta1403 4 жыл бұрын
@@techdose4u You are doing a great job sir. Could you please share your FB Id so that i can connect.
@PhenomenalInitiations
@PhenomenalInitiations 3 жыл бұрын
getting TLE with set
@shivanshujaiswal7635
@shivanshujaiswal7635 4 жыл бұрын
Hi please explain BIT method
@techdose4u
@techdose4u 4 жыл бұрын
Sure.
@AnkitKumarCoder
@AnkitKumarCoder 3 жыл бұрын
From where I get the code?
@techdose4u
@techdose4u 3 жыл бұрын
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.
@rajneeshyadav9728
@rajneeshyadav9728 4 жыл бұрын
can you explain the BIT method
@techdose4u
@techdose4u 4 жыл бұрын
Yea....I will cover this problem when I cover BIT
@purushottamkumar3140
@purushottamkumar3140 2 жыл бұрын
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
@ROSHANKUMAR-rl6bf
@ROSHANKUMAR-rl6bf 3 жыл бұрын
multiset me minus nhi hota h sir
@shivangitomar5557
@shivangitomar5557 3 жыл бұрын
3rd method should be O(nlogn) right? ( we dont need to use "distance")
@nishayadav9988
@nishayadav9988 3 жыл бұрын
sir please also provide a code of this problem
@techdose4u
@techdose4u 3 жыл бұрын
Please try to code yourself
@nishayadav9988
@nishayadav9988 3 жыл бұрын
Ok sir
@ROSHANKUMAR-rl6bf
@ROSHANKUMAR-rl6bf 3 жыл бұрын
difference kaise nikale
@priyabratasingha174
@priyabratasingha174 2 жыл бұрын
I think in the method 3 ,the time complexity should be o(nlogn).
@soumyadeepdash4805
@soumyadeepdash4805 2 жыл бұрын
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)
@schan263
@schan263 9 ай бұрын
I know the solution but not the intuition that leads to merge sort.
@mtr2936
@mtr2936 3 жыл бұрын
Sir can you share the source code
@techdose4u
@techdose4u 3 жыл бұрын
Please try to search in comment section
@margolikesbees
@margolikesbees 3 жыл бұрын
:)
@ShivamGupta-cx3hy
@ShivamGupta-cx3hy 3 жыл бұрын
you should change your name to Placement Cracker
@techdose4u
@techdose4u 3 жыл бұрын
Let it be techdose :P
@ShivamGupta-cx3hy
@ShivamGupta-cx3hy 3 жыл бұрын
@@techdose4u Thank you So much ,Sir you are a great help .
@aneeshkulkarni1739
@aneeshkulkarni1739 4 жыл бұрын
At 3:10 you accidentally said 4 is greater than 6 hence there is inversion. Got confused for a bit 😛
@techdose4u
@techdose4u 4 жыл бұрын
:P
@aneeshkulkarni1739
@aneeshkulkarni1739 4 жыл бұрын
Great explanation! Could you also make a video about how to get the number of inversions per element in the original array?
@techdose4u
@techdose4u 4 жыл бұрын
Sure.....but can you suggest me a proper title for that. That will be helpful to see if users need it or not.
@aneeshkulkarni1739
@aneeshkulkarni1739 4 жыл бұрын
How about "count of smaller numbers after self" ?
@techdose4u
@techdose4u 4 жыл бұрын
I guess I should have explained that in video itself 😅
@rohitpal7739
@rohitpal7739 4 жыл бұрын
maal
@purushottamkumar3140
@purushottamkumar3140 2 жыл бұрын
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
Count Inversions in Array
11:48
Code with Alisha
Рет қаралды 10 М.
Trapping Rainwater Problem | Leetcode #42
34:12
Techdose
Рет қаралды 96 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 207 МЛН
Inside Out 2: Who is the strongest? Joy vs Envy vs Anger #shorts #animation
00:22
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 33 МЛН
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН
Rearrange array alternately
10:37
Techdose
Рет қаралды 73 М.
Largest number formed from an array
8:33
Techdose
Рет қаралды 117 М.
Longest increasing subsequence
20:44
Techdose
Рет қаралды 127 М.
Find equilibrium point in an array
10:32
Techdose
Рет қаралды 54 М.
2.6 - Counting Inversions in an Array in O(n log n) time via Divide and Conquer
19:27
Algorithms by Sharma Thankachan
Рет қаралды 10 М.
Candy distribution problem
15:54
Techdose
Рет қаралды 95 М.
The BEST Way to Find a Random Point in a Circle | #SoME1 #3b1b
18:35
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 207 МЛН