Segregate 0's, 1's and 2's together in an array[O(n)](Dutch National Flag Problem)

  Рет қаралды 61,335

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

Күн бұрын

Пікірлер: 123
@dongiveajack
@dongiveajack 3 жыл бұрын
Khyade sir is the BEST
@lien-chinwei4815
@lien-chinwei4815 3 жыл бұрын
Thank you for demonstrating all the detail and steps many algorithm books skip or leave exercises to readers.
@sjwang3892
@sjwang3892 3 жыл бұрын
The only video that explained this clearly to me. Thank you!
@arunm619
@arunm619 5 жыл бұрын
1:14 Not gonna lie, Thought Thanos snapped in the first half
@ashishmishra8082
@ashishmishra8082 5 жыл бұрын
You explained it as a specific example of segregating 0,1 and 2 but it helped me to understand 3 way partition of Quicksort. Thanks sir
@jainmanan9182
@jainmanan9182 3 жыл бұрын
Thankyou Sir, best explanation on KZbin :)
@RajaRam-fv1mm
@RajaRam-fv1mm 3 жыл бұрын
Really a detailed explanation that makes more sense. Thank you so Much Sir
@Vishal-nc8ju
@Vishal-nc8ju 5 жыл бұрын
best channel on yt to learn and improve skills from basics
@praveenchouhan6388
@praveenchouhan6388 4 жыл бұрын
awesome explaination !!!!!!!! love the clarity of ur approach and visual analysis.
@skubydubydu
@skubydubydu 5 жыл бұрын
Counting method also takes O(n) time. It is also taking least time. The reason the swapping method is asked in interviews is because in real scenarios the actual data set is not going to contain just simple numbers. It will be made of records which need to be sorted based on one of the properties of the records.
@GurpreetSingh-yy5fn
@GurpreetSingh-yy5fn 4 жыл бұрын
counting method takes two traversals while this algorithm takes only one traversal
@udaykadam5455
@udaykadam5455 4 жыл бұрын
Doesn't matter even if the data is in the string format. You can still count the number of occurrences. It's just that, interviewer will ask for another solution. Combination of double pointer approach along with hoare partion algo can solve this problem within linear time.
@AvinashKumar-ps8wl
@AvinashKumar-ps8wl 3 жыл бұрын
Best Explaination on KZbin
@abhisheksarkar_neo
@abhisheksarkar_neo 7 жыл бұрын
As proposed at the start of the video, counting is also a way to do it and that will also take O(n) time. The reason we cannot do that is because, it will not work in cases where you have any set of numbers, not just 0, 1 and 2 and you want to partition the numbers in 3 buckets
@sravanvedala7811
@sravanvedala7811 7 жыл бұрын
well even with this approach also, you need as many buckets as that of your number groups. For example if you have 0 to 5 numbers repeated in an array, then you need to modify this algorithm to address grouping of 0 to5 numbers in which you case you end up having 6 buckets even with this algorithm. This won't work out of the box if your array has numbers other than 0,1 and 2. However caveat with just counting is, you need another array.
@spetsnaz_2
@spetsnaz_2 5 жыл бұрын
If you do it by counting, then it will take 2 passes to complete the program, but this algo will do the job in a single pass. And this algo is only made for this kind of problems...if we have more no. of distinct elements, why not to use other sorting techniques :P
@krishnataramanandhar581
@krishnataramanandhar581 2 жыл бұрын
Thank you for the video that clearly explains how to segregate different value elements,
@ThinkProgramming-Hub
@ThinkProgramming-Hub Жыл бұрын
A very great and detailed explanation🤝, you make learning fun👏
@niklasschulte1690
@niklasschulte1690 6 жыл бұрын
very nice and clean explaned, thanks!
@JayDonga
@JayDonga 4 жыл бұрын
You demonstrated algorithm and it works really well. However if someone has never encountered this solution before and they get this problem in interview, in that case how to develop this algorithm from scratch? Appreciate your inputs.
@ankitchawla8487
@ankitchawla8487 4 жыл бұрын
Good Explanation. Very easy to understand thanks a lot for making this video.
@adityagunda2122
@adityagunda2122 3 жыл бұрын
awesome explanation. easy to understand
@aishwaryasingh246
@aishwaryasingh246 5 жыл бұрын
Amazing content! Thanks for the algorithm videos. It has helped me a lot.
@Akash-Bisariya
@Akash-Bisariya 3 жыл бұрын
How easy you made this problem... Wow!!!
@vivekanandkhyade
@vivekanandkhyade 3 жыл бұрын
Thanks a lot
@Badatbackflips
@Badatbackflips 4 жыл бұрын
thank you for the very clear (and concise!) explanation
@angitd3766
@angitd3766 7 жыл бұрын
Best explaination so far
@minirasamedova648
@minirasamedova648 3 жыл бұрын
Please keep them coming!!
@CASHTECH1234
@CASHTECH1234 4 жыл бұрын
Good video and explanation. The first 5 mins are sufficient if you already have some grip on algorithms
@hiteshusingh8571
@hiteshusingh8571 4 жыл бұрын
Very nice explanation
@chaoschao9432
@chaoschao9432 6 жыл бұрын
This is related to leetcode problem 75.
@rahulchandrabhan
@rahulchandrabhan 4 жыл бұрын
very easy explanation..Nice
@DurgaShiva7574
@DurgaShiva7574 3 жыл бұрын
could the while condition be changed to while(mid
@subrinasirajee530
@subrinasirajee530 3 жыл бұрын
Well explained. Thanks a lot.
@vivekanandkhyade
@vivekanandkhyade 3 жыл бұрын
Glad it was helpful!
@kewtomrao
@kewtomrao 3 жыл бұрын
So mid travels through the array and when 1 is encountered,we allow it to be in its place but if its 0 we swap it to the left end of 1 ie with the low index and inc low by 1 since the 0 has been placed on the right.But if its greater than 1 then we swap it with the high index ie to the right of 1 and dec high since the 2 has already been put to the end.
@amishapurswani9957
@amishapurswani9957 4 жыл бұрын
greatly explained. thanku sir
@sasmitasahoo1161
@sasmitasahoo1161 4 жыл бұрын
Nicely explained
@Bakepichai
@Bakepichai 7 жыл бұрын
Useful for lot of people. Keep going
@prnk139
@prnk139 4 жыл бұрын
very impressive way of explaining algorithm !!
@colettecampbell1528
@colettecampbell1528 3 жыл бұрын
Thank you! I finally understand.
@rajatmishra3572
@rajatmishra3572 4 жыл бұрын
Thank You sir !!
@deepakdas4224
@deepakdas4224 5 жыл бұрын
Use Counting sorting for this problem that would be helpful with O(1) space complexity
@ThotaMadhuSudhanRao
@ThotaMadhuSudhanRao 5 жыл бұрын
But in interview they say not to use that approach and do swapping based
@DurgaShiva7574
@DurgaShiva7574 3 жыл бұрын
also Vivekanad Sir...as you said that u can make videos as per ur subscribers request ..so please explain the theory then programming/code of Edges and graphs...as every company is asking it..and i am totally blank for it..hoping to hear from u soon
@anshulsaini7440
@anshulsaini7440 2 жыл бұрын
Thankyou sir 🙏
@sholihatulrichas9414
@sholihatulrichas9414 6 жыл бұрын
Thank you, your videos are very helpful!!! Keep going
@sukritkapil9816
@sukritkapil9816 3 жыл бұрын
nicely explained! thanks
@auritroghoshal8469
@auritroghoshal8469 2 жыл бұрын
Thank You Sir.
@neharikakhera6411
@neharikakhera6411 4 жыл бұрын
Thanks for sharing
@adityapaithon6499
@adityapaithon6499 4 жыл бұрын
Good explanation
@sentinel-y8l
@sentinel-y8l 5 жыл бұрын
When a[mid] == 2, a[high], besides being 0 or 1 can also be 2, which would need to be moved (like the 0 you mentioned). You simply know nothing about a[high] because you never processed that value before, as opposed to a[low] (which was processed before, so it can only be 0 - first iteration, or 1 all other iterations). This is due to you starting mid iterations from left to right.
@Anubis10110
@Anubis10110 4 жыл бұрын
Thanks man..clear explanation..
@davidotewa6411
@davidotewa6411 4 жыл бұрын
Anyone knows how to find the algorithm if we decide to have Four numbers instead of 3. Would we use the above approach
@letstalk8670
@letstalk8670 3 жыл бұрын
Sir please make separate playlist for all the topics..
@MrMarkgyuro
@MrMarkgyuro 5 жыл бұрын
King of Algorithm's!
@shailshah9021
@shailshah9021 6 жыл бұрын
Counting the number of 0s, 1s, and 2s and then replacing the array is also O(n). However, it is not in-place.
@abhimanyusharma3741
@abhimanyusharma3741 6 жыл бұрын
It will also take 2 pass to sort the array. Dutch national flag will sort the array in 1 pass.
@gamma_v1
@gamma_v1 4 жыл бұрын
It's actually in-place (see the definition of in-place: en.wikipedia.org/wiki/In-place_algorithm). The reason this method is not acceptable for this problem is that by definition of the problem you are only allowed to swap elements (see the definition of Dutch National Flag Problem in Wikipedia).
@shofiyabootwala2094
@shofiyabootwala2094 3 жыл бұрын
Thank You Sir
@shreyanaik6062
@shreyanaik6062 4 жыл бұрын
Thank you so much sir
@TechieAlpha
@TechieAlpha 3 жыл бұрын
Thank you sir 🖤
@farazahmed6043
@farazahmed6043 6 жыл бұрын
Amazing sir. Keeping making such vids
@deepakdubey519
@deepakdubey519 5 жыл бұрын
I wish I had taken your lectures before
@anjurawat9274
@anjurawat9274 4 жыл бұрын
nice explanation but counting 1 2 3 can also be done in 0(n) so how this one more efficient?
@sumansaurabh8105
@sumansaurabh8105 4 жыл бұрын
efficiency here is that it does all that in the single pass.
@anjurawat9274
@anjurawat9274 4 жыл бұрын
@@sumansaurabh8105 oh u meant we can't calculate 1,2,0 count in one pass?
@sumansaurabh8105
@sumansaurabh8105 4 жыл бұрын
You can but then in one pass you will count all the 0,1 and 2s and then you will probably make another array to populate those or go one more pass to populate the original array with 0,1 and 2. The challenge here is you have to do in memory without using extra space and in one pass. Hence this solution. I would recommend you to go too leetcode and read the problem description.
@anjurawat9274
@anjurawat9274 4 жыл бұрын
@@sumansaurabh8105 satisfied thanks
@nikhilm4290
@nikhilm4290 4 жыл бұрын
@suman once we count the number of 0's,1's and 2's why do we place in an extra array. We can just override the elements in given array since we know the count of each. I understand it takes O(2n)=>O(n) but as long as the interviewer is happy we can go with count solution. It is always not necessary to give text book solutions and it is a very natural way to think about the count initially even before you propose this textbook solution. I hope you agree with this.
@alexanderpeles81
@alexanderpeles81 7 жыл бұрын
Thank you. Very nice explanation
@coderajput1816
@coderajput1816 7 жыл бұрын
very good explanation
@ah62mousavi
@ah62mousavi 6 жыл бұрын
nice job, do you have the verification video of this? pre and post condition and loop invariants?
@dewdropk-dl5tv
@dewdropk-dl5tv 7 жыл бұрын
very nice explanation
@infotainment7123
@infotainment7123 4 жыл бұрын
you got new suscriber
@lokeshagarwal6219
@lokeshagarwal6219 4 жыл бұрын
swap part i m not able to understand like how we can shift from case 0 to case 1,2
@babu123456781
@babu123456781 7 жыл бұрын
Why didnt we use counting sort for this problem? Counting sort takes O(n) Time complexity. It uses extra space though.
@simranjeetsingh6002
@simranjeetsingh6002 6 жыл бұрын
In counting sort you need to traverse the array twice (first time for counting and second time for putting) ,so its time complexity is O(2n) but in this one only once you have to traverse so its purely O(n)
@gurmeetchawla8362
@gurmeetchawla8362 6 жыл бұрын
simran O(2n) is also O(n) only, I am sure you know that, but I am not trying to justify using of count sort for this problem.
@adarshpanigrahi7777
@adarshpanigrahi7777 4 жыл бұрын
Sir can we use sorting alogrithm I would be very easy than any other method
@GurpreetSingh-yy5fn
@GurpreetSingh-yy5fn 4 жыл бұрын
in the interviews they want us to solve it without sorting, so this algorithm is used.
@mayankaggarwal2937
@mayankaggarwal2937 5 жыл бұрын
i have an question .. why you don't just sorted the elements using merge sort.. because for worst case also..its complexity is o(nlogn)..and in this video you are getting o(n) complexity.i think merge sort would be a very nycc option .can you please explain..
@ankurshah3112
@ankurshah3112 5 жыл бұрын
You can do it in O(n)via countSort in 2 pass,but this is trying to do it in one pass. Question has this constraint. If somebody asks you to do it in 1 pass, this is how you would solve it.
@bigray712
@bigray712 7 жыл бұрын
amazing
@AdityaKumar-ws5pv
@AdityaKumar-ws5pv 7 жыл бұрын
awesome sir ji u r gr8.
@sourovroy7951
@sourovroy7951 5 жыл бұрын
The Solution is buggy. It will not pass [1,2,0]. Try this one: void sort(vector& nums) { int left=0,mid=0,right=nums.size()-1; while(mid
@SoumikNandi
@SoumikNandi 4 жыл бұрын
Godly
@rushi901
@rushi901 7 жыл бұрын
Hi, Can you make video for median of two sorted array or median of stream?
@lazy_engineer
@lazy_engineer 6 жыл бұрын
very useful thanks
@Ram-kv1xp
@Ram-kv1xp 4 жыл бұрын
@ 5:30 look at the Flicka Da Wrist
@dasabhishek0790
@dasabhishek0790 5 жыл бұрын
Can you show this for 0,1,2,0,1,2 ??? I think this will fail.
@monalisadas9062
@monalisadas9062 5 жыл бұрын
and even for [1,2,0]
@prajitbanerjee8226
@prajitbanerjee8226 5 жыл бұрын
nice
@KnowledgeActionWords
@KnowledgeActionWords 5 жыл бұрын
thanks
@rahulkhandelwal46578
@rahulkhandelwal46578 7 жыл бұрын
Thank You
@vineetktripathi
@vineetktripathi 6 жыл бұрын
Very good explanation, but naming of mid is confusing, Why don't you rename mid to tracker / pointer/currentIndex.
@vaibhavrbs
@vaibhavrbs 5 жыл бұрын
I was thinking the same, however if you think about it, calling it mid makes more sense as mid_value_index as it stays with 1, similarly with high_value_index as it stays with 2 and same case with low_value_index as it stays with 0 ,
@payalsagar1808
@payalsagar1808 4 жыл бұрын
thankgod he is there :) saviour
@good114
@good114 Жыл бұрын
💞💞❤❤
@sachinbhalla14
@sachinbhalla14 5 жыл бұрын
sir your code is not working can any one provide source code ??
@chaoschao9432
@chaoschao9432 6 жыл бұрын
Whether this algorithim is in place?
@yatheeshareddy3661
@yatheeshareddy3661 5 жыл бұрын
no, it is a kind of quicksort algo
@rishabhbisht7014
@rishabhbisht7014 5 жыл бұрын
This didn't work for me when passed an array = [1,0,1,0,1,0,1,0,2,0,2,0,2,0,2,0,2,1,0,1,0,1,0]
@arnavrastogi8473
@arnavrastogi8473 4 жыл бұрын
Problem : code logic is not correct solution: Add mid
@sanjaysingh5007
@sanjaysingh5007 6 жыл бұрын
sir vector pairs , DSU,
@sumanthv2046
@sumanthv2046 6 жыл бұрын
This wont work for {0,1,2,1,2} because we arent finding the crct values of low,mid and high initially.
@pratikparikh8027
@pratikparikh8027 6 жыл бұрын
It works
@chandangarg1328
@chandangarg1328 4 жыл бұрын
but counting will also take O(n)
@kansiram5340
@kansiram5340 7 жыл бұрын
sir i need your github link
@abhishektiwari5932
@abhishektiwari5932 3 жыл бұрын
print(arr.sort())
@SIG442
@SIG442 6 жыл бұрын
Yet your own flag, India I take it, is based up on the Dutch flag like so many other nations. That would mean that your own flag has a problem as well ;) Aside from that, I tried to follow what you were saying. But I have no clue still what you are trying to tell with this. Please do explain. I can't say I had any useful education at school, along side that I was the target for pretty much everyone. So learning wasn't really my primary thing to do in my early years. Yet I seem to be smart enough to follow very advanced things in many cases, with the right explanation. I like to learn and understand more.
@tusharbhart4105
@tusharbhart4105 4 жыл бұрын
Just sort the array in ascending order......that's it
@Matkartik-yz4vs
@Matkartik-yz4vs 5 жыл бұрын
very slow...
@stholy32
@stholy32 5 жыл бұрын
The only question i have is....... Where on earth will you ever use this.....???!!
@spetsnaz_2
@spetsnaz_2 5 жыл бұрын
The place where you got to know this algo!
@sachinbhalla14
@sachinbhalla14 5 жыл бұрын
sir your code is not working can any one provide source code ??
Pairs with given sum in an array (code/Algorithm)
7:11
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 61 М.
Maximum Sum SubArray (Kadane's algorithm) (Largest Sum Contigous SubArray)
17:30
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 92 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,4 МЛН
Leader in an Array (Code / Algorithm)
15:24
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 21 М.
Minimum jumps to reach end of array (Dynamic Programming)
26:46
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 85 М.
Staircase Problem (Dynamic Programming) Fibonacci Series pattern
15:06
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 28 М.
Dutch National Flag Algorithm. Explained with playing cards.
12:11
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 21 МЛН
The Dutch National Flag Problem (The Quicksort "Band-Aid")
10:54
Back To Back SWE
Рет қаралды 31 М.
Find missing number in an array(using summation and XOR operation)
8:59
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 42 М.