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

Segregate 0's, 1's and 2's together in an array[O(n)](Dutch National Flag Problem). Most efficient solution for 3 way partitioning.

Пікірлер: 123

  • @dongiveajack
    @dongiveajack3 жыл бұрын

    Khyade sir is the BEST

  • @lien-chinwei4815
    @lien-chinwei48153 жыл бұрын

    Thank you for demonstrating all the detail and steps many algorithm books skip or leave exercises to readers.

  • @arunm619
    @arunm6195 жыл бұрын

    1:14 Not gonna lie, Thought Thanos snapped in the first half

  • @sjwang3892
    @sjwang38923 жыл бұрын

    The only video that explained this clearly to me. Thank you!

  • @jainmanan9182
    @jainmanan91823 жыл бұрын

    Thankyou Sir, best explanation on KZread :)

  • @ashishmishra8082
    @ashishmishra80824 жыл бұрын

    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

  • @praveenchouhan6388
    @praveenchouhan63884 жыл бұрын

    awesome explaination !!!!!!!! love the clarity of ur approach and visual analysis.

  • @Vishal-nc8ju
    @Vishal-nc8ju4 жыл бұрын

    best channel on yt to learn and improve skills from basics

  • @AvinashKumar-ps8wl
    @AvinashKumar-ps8wl2 жыл бұрын

    Best Explaination on KZread

  • @RajaRam-fv1mm
    @RajaRam-fv1mm2 жыл бұрын

    Really a detailed explanation that makes more sense. Thank you so Much Sir

  • @ThinkProgramming-Hub
    @ThinkProgramming-Hub9 ай бұрын

    A very great and detailed explanation🤝, you make learning fun👏

  • @niklasschulte1690
    @niklasschulte16906 жыл бұрын

    very nice and clean explaned, thanks!

  • @aishwaryasingh246
    @aishwaryasingh2464 жыл бұрын

    Amazing content! Thanks for the algorithm videos. It has helped me a lot.

  • @Badatbackflips
    @Badatbackflips4 жыл бұрын

    thank you for the very clear (and concise!) explanation

  • @sholihatulrichas9414
    @sholihatulrichas94146 жыл бұрын

    Thank you, your videos are very helpful!!! Keep going

  • @krishnataramanandhar581
    @krishnataramanandhar5812 жыл бұрын

    Thank you for the video that clearly explains how to segregate different value elements,

  • @ankitchawla8487
    @ankitchawla84874 жыл бұрын

    Good Explanation. Very easy to understand thanks a lot for making this video.

  • @ah62mousavi
    @ah62mousavi6 жыл бұрын

    nice job, do you have the verification video of this? pre and post condition and loop invariants?

  • @prnk139
    @prnk1394 жыл бұрын

    very impressive way of explaining algorithm !!

  • @farazahmed6043
    @farazahmed60436 жыл бұрын

    Amazing sir. Keeping making such vids

  • @adityagunda2122
    @adityagunda21223 жыл бұрын

    awesome explanation. easy to understand

  • @rushi901
    @rushi9016 жыл бұрын

    Hi, Can you make video for median of two sorted array or median of stream?

  • @kewtomrao
    @kewtomrao3 жыл бұрын

    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.

  • @Akash-Bisariya
    @Akash-Bisariya2 жыл бұрын

    How easy you made this problem... Wow!!!

  • @vivekanandkhyade

    @vivekanandkhyade

    2 жыл бұрын

    Thanks a lot

  • @davidotewa6411
    @davidotewa64113 жыл бұрын

    Anyone knows how to find the algorithm if we decide to have Four numbers instead of 3. Would we use the above approach

  • @alexanderpeles81
    @alexanderpeles817 жыл бұрын

    Thank you. Very nice explanation

  • @JayDonga
    @JayDonga4 жыл бұрын

    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.

  • @lokeshagarwal6219
    @lokeshagarwal62194 жыл бұрын

    swap part i m not able to understand like how we can shift from case 0 to case 1,2

  • @DurgaShiva7574
    @DurgaShiva75743 жыл бұрын

    could the while condition be changed to while(mid

  • @CASHTECH1234
    @CASHTECH12343 жыл бұрын

    Good video and explanation. The first 5 mins are sufficient if you already have some grip on algorithms

  • @minirasamedova648
    @minirasamedova6483 жыл бұрын

    Please keep them coming!!

  • @skubydubydu
    @skubydubydu4 жыл бұрын

    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

    3 жыл бұрын

    counting method takes two traversals while this algorithm takes only one traversal

  • @udaykadam5455

    @udaykadam5455

    3 жыл бұрын

    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.

  • @colettecampbell1528
    @colettecampbell15283 жыл бұрын

    Thank you! I finally understand.

  • @AdityaKumar-ws5pv
    @AdityaKumar-ws5pv7 жыл бұрын

    awesome sir ji u r gr8.

  • @coderajput1816
    @coderajput18166 жыл бұрын

    very good explanation

  • @amishapurswani9957
    @amishapurswani99573 жыл бұрын

    greatly explained. thanku sir

  • @sukritkapil9816
    @sukritkapil98163 жыл бұрын

    nicely explained! thanks

  • @hiteshusingh8571
    @hiteshusingh85713 жыл бұрын

    Very nice explanation

  • @Anubis10110
    @Anubis101104 жыл бұрын

    Thanks man..clear explanation..

  • @dewdropk-dl5tv
    @dewdropk-dl5tv6 жыл бұрын

    very nice explanation

  • @Bakepichai
    @Bakepichai6 жыл бұрын

    Useful for lot of people. Keep going

  • @abhisheksarkar_neo
    @abhisheksarkar_neo7 жыл бұрын

    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

  • @angitd3766
    @angitd37666 жыл бұрын

    Best explaination so far

  • @rahulchandrabhan
    @rahulchandrabhan3 жыл бұрын

    very easy explanation..Nice

  • @subrinasirajee530
    @subrinasirajee5303 жыл бұрын

    Well explained. Thanks a lot.

  • @vivekanandkhyade

    @vivekanandkhyade

    3 жыл бұрын

    Glad it was helpful!

  • @sasmitasahoo1161
    @sasmitasahoo11614 жыл бұрын

    Nicely explained

  • @rajatmishra3572
    @rajatmishra35724 жыл бұрын

    Thank You sir !!

  • @lazy_engineer
    @lazy_engineer6 жыл бұрын

    very useful thanks

  • @TechieAlpha
    @TechieAlpha3 жыл бұрын

    Thank you sir 🖤

  • @auritroghoshal8469
    @auritroghoshal8469 Жыл бұрын

    Thank You Sir.

  • @neharikakhera6411
    @neharikakhera64114 жыл бұрын

    Thanks for sharing

  • @bigray712
    @bigray7127 жыл бұрын

    amazing

  • @adityapaithon6499
    @adityapaithon64994 жыл бұрын

    Good explanation

  • @DurgaShiva7574
    @DurgaShiva75743 жыл бұрын

    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

  • @shofiyabootwala2094
    @shofiyabootwala20943 жыл бұрын

    Thank You Sir

  • @rahulkhandelwal46578
    @rahulkhandelwal465786 жыл бұрын

    Thank You

  • @sanjaysingh5007
    @sanjaysingh50076 жыл бұрын

    sir vector pairs , DSU,

  • @shreyanaik6062
    @shreyanaik60624 жыл бұрын

    Thank you so much sir

  • @anshulsaini7440
    @anshulsaini74402 жыл бұрын

    Thankyou sir 🙏

  • @deepakdubey519
    @deepakdubey5194 жыл бұрын

    I wish I had taken your lectures before

  • @mayankaggarwal2937
    @mayankaggarwal29375 жыл бұрын

    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.

  • @sachinbhalla14
    @sachinbhalla145 жыл бұрын

    sir your code is not working can any one provide source code ??

  • @babu123456781
    @babu1234567817 жыл бұрын

    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.

  • @chaoschao9432
    @chaoschao94325 жыл бұрын

    This is related to leetcode problem 75.

  • @kansiram5340
    @kansiram53407 жыл бұрын

    sir i need your github link

  • @MrMarkgyuro
    @MrMarkgyuro4 жыл бұрын

    King of Algorithm's!

  • @deepakdas4224
    @deepakdas42245 жыл бұрын

    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

  • @adarshpanigrahi7777
    @adarshpanigrahi77774 жыл бұрын

    Sir can we use sorting alogrithm I would be very easy than any other method

  • @GurpreetSingh-yy5fn

    @GurpreetSingh-yy5fn

    3 жыл бұрын

    in the interviews they want us to solve it without sorting, so this algorithm is used.

  • @shailshah9021
    @shailshah90216 жыл бұрын

    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

    5 жыл бұрын

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

  • @infotainment7123
    @infotainment71234 жыл бұрын

    you got new suscriber

  • @KnowledgeActionWords
    @KnowledgeActionWords5 жыл бұрын

    thanks

  • @andrepinto7895
    @andrepinto78954 жыл бұрын

    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.

  • @letstalk8670
    @letstalk86703 жыл бұрын

    Sir please make separate playlist for all the topics..

  • @payalsagar1808
    @payalsagar18084 жыл бұрын

    thankgod he is there :) saviour

  • @anjurawat9274
    @anjurawat92744 жыл бұрын

    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

    3 жыл бұрын

    @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.

  • @prajitbanerjee8226
    @prajitbanerjee82265 жыл бұрын

    nice

  • @sourovroy7951
    @sourovroy79515 жыл бұрын

    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

  • @dasabhishek0790
    @dasabhishek07904 жыл бұрын

    Can you show this for 0,1,2,0,1,2 ??? I think this will fail.

  • @monalisadas9062

    @monalisadas9062

    4 жыл бұрын

    and even for [1,2,0]

  • @SoumikNandi
    @SoumikNandi4 жыл бұрын

    Godly

  • @vineetktripathi
    @vineetktripathi6 жыл бұрын

    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 ,

  • @good114
    @good1148 ай бұрын

    💞💞❤❤

  • @rishabhbisht7014
    @rishabhbisht70145 жыл бұрын

    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

  • @Ram-kv1xp
    @Ram-kv1xp4 жыл бұрын

    @ 5:30 look at the Flicka Da Wrist

  • @chandangarg1328
    @chandangarg13284 жыл бұрын

    but counting will also take O(n)

  • @chaoschao9432
    @chaoschao94325 жыл бұрын

    Whether this algorithim is in place?

  • @yatheeshareddy3661

    @yatheeshareddy3661

    5 жыл бұрын

    no, it is a kind of quicksort algo

  • @abhishektiwari5932
    @abhishektiwari59323 жыл бұрын

    print(arr.sort())

  • @sumanthv2046
    @sumanthv20465 жыл бұрын

    This wont work for {0,1,2,1,2} because we arent finding the crct values of low,mid and high initially.

  • @pratikparikh8027

    @pratikparikh8027

    5 жыл бұрын

    It works

  • @SIG442
    @SIG4426 жыл бұрын

    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.

  • @Matkartik-yz4vs
    @Matkartik-yz4vs5 жыл бұрын

    very slow...

  • @stholy32
    @stholy325 жыл бұрын

    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!

  • @tusharbhart4105
    @tusharbhart41053 жыл бұрын

    Just sort the array in ascending order......that's it

  • @sachinbhalla14
    @sachinbhalla145 жыл бұрын

    sir your code is not working can any one provide source code ??