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

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 123

  • @dongiveajack
    @dongiveajack 4 года назад +9

    Khyade sir is the BEST

  • @sjwang3892
    @sjwang3892 3 года назад +5

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

  • @lien-chinwei4815
    @lien-chinwei4815 4 года назад +7

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

  • @ashishmishra8082
    @ashishmishra8082 5 лет назад +2

    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

  • @arunm619
    @arunm619 5 лет назад +29

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

  • @jainmanan9182
    @jainmanan9182 3 года назад +4

    Thankyou Sir, best explanation on RUclips :)

  • @Vishal-nc8ju
    @Vishal-nc8ju 5 лет назад +1

    best channel on yt to learn and improve skills from basics

  • @RajaRam-AutomationTesting
    @RajaRam-AutomationTesting 3 года назад +1

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

  • @praveenchouhan6388
    @praveenchouhan6388 4 года назад +1

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

  • @AvinashKumar-ps8wl
    @AvinashKumar-ps8wl 3 года назад

    Best Explaination on RUclips

  • @niklasschulte1690
    @niklasschulte1690 6 лет назад +14

    very nice and clean explaned, thanks!

  • @aishwaryasingh246
    @aishwaryasingh246 5 лет назад +5

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

  • @ThinkProgramming-Hub
    @ThinkProgramming-Hub Год назад

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

  • @krishnataramanandhar581
    @krishnataramanandhar581 3 года назад

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

  • @skubydubydu
    @skubydubydu 5 лет назад +2

    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.

  • @JayDonga
    @JayDonga 4 года назад +2

    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.

  • @abhisheksarkar_neo
    @abhisheksarkar_neo 7 лет назад +2

    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

  • @Badatbackflips
    @Badatbackflips 4 года назад

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

  • @adityagunda2122
    @adityagunda2122 4 года назад

    awesome explanation. easy to understand

  • @sholihatulrichas9414
    @sholihatulrichas9414 6 лет назад +2

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

  • @CASHTECH1234
    @CASHTECH1234 4 года назад

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

  • @Akash-Bisariya
    @Akash-Bisariya 3 года назад +1

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

  • @angitd3766
    @angitd3766 7 лет назад +3

    Best explaination so far

  • @colettecampbell1528
    @colettecampbell1528 3 года назад

    Thank you! I finally understand.

  • @minirasamedova648
    @minirasamedova648 3 года назад

    Please keep them coming!!

  • @subrinasirajee530
    @subrinasirajee530 4 года назад +1

    Well explained. Thanks a lot.

  • @hiteshusingh8571
    @hiteshusingh8571 4 года назад

    Very nice explanation

  • @rahulchandrabhan
    @rahulchandrabhan 4 года назад

    very easy explanation..Nice

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

  • @Bakepichai
    @Bakepichai 7 лет назад +5

    Useful for lot of people. Keep going

  • @prnk139
    @prnk139 5 лет назад

    very impressive way of explaining algorithm !!

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

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

  • @anshulsaini7440
    @anshulsaini7440 3 года назад +1

    Thankyou sir 🙏

  • @DurgaShiva7574
    @DurgaShiva7574 3 года назад

    could the while condition be changed to while(mid

  • @Anubis10110
    @Anubis10110 4 года назад

    Thanks man..clear explanation..

  • @rajatmishra3572
    @rajatmishra3572 4 года назад +2

    Thank You sir !!

  • @sasmitasahoo1161
    @sasmitasahoo1161 4 года назад

    Nicely explained

  • @anjurawat9274
    @anjurawat9274 4 года назад +1

    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 года назад +2

      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 года назад +1

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

  • @amishapurswani9957
    @amishapurswani9957 4 года назад

    greatly explained. thanku sir

  • @farazahmed6043
    @farazahmed6043 6 лет назад

    Amazing sir. Keeping making such vids

  • @TechieAlpha
    @TechieAlpha 4 года назад

    Thank you sir 🖤

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

  • @mayankaggarwal2937
    @mayankaggarwal2937 6 лет назад

    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 6 лет назад

      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.

  • @shailshah9021
    @shailshah9021 6 лет назад +1

    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 лет назад +2

      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 года назад +1

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

  • @sukritkapil9816
    @sukritkapil9816 4 года назад

    nicely explained! thanks

  • @alexanderpeles81
    @alexanderpeles81 7 лет назад

    Thank you. Very nice explanation

  • @deepakdas4224
    @deepakdas4224 5 лет назад +1

    Use Counting sorting for this problem that would be helpful with O(1) space complexity

    • @ThotaMadhuSudhanRao
      @ThotaMadhuSudhanRao 5 лет назад +3

      But in interview they say not to use that approach and do swapping based

  • @adityapaithon6499
    @adityapaithon6499 4 года назад

    Good 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

  • @shreyanaik6062
    @shreyanaik6062 4 года назад

    Thank you so much sir

  • @auritroghoshal8469
    @auritroghoshal8469 2 года назад

    Thank You Sir.

  • @chaoschao9432
    @chaoschao9432 6 лет назад +4

    This is related to leetcode problem 75.

  • @neharikakhera6411
    @neharikakhera6411 4 года назад

    Thanks for sharing

  • @letstalk8670
    @letstalk8670 3 года назад

    Sir please make separate playlist for all the topics..

  • @MrMarkgyuro
    @MrMarkgyuro 5 лет назад +1

    King of Algorithm's!

  • @shofiyabootwala2094
    @shofiyabootwala2094 3 года назад

    Thank You Sir

  • @deepakdubey519
    @deepakdubey519 5 лет назад

    I wish I had taken your lectures before

  • @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 лет назад +1

      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 года назад +1

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

  • @infotainment7123
    @infotainment7123 4 года назад

    you got new suscriber

  • @coderajput1816
    @coderajput1816 7 лет назад

    very good explanation

  • @AdityaKumar-ws5pv
    @AdityaKumar-ws5pv 7 лет назад

    awesome sir ji u r gr8.

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

  • @dasabhishek0790
    @dasabhishek0790 5 лет назад

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

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

  • @payalsagar1808
    @payalsagar1808 4 года назад

    thankgod he is there :) saviour

  • @bigray712
    @bigray712 7 лет назад +1

    amazing

  • @good114
    @good114 Год назад

    💞💞❤❤

  • @vineetktripathi
    @vineetktripathi 7 лет назад +1

    Very good explanation, but naming of mid is confusing, Why don't you rename mid to tracker / pointer/currentIndex.

    • @vaibhavrbs
      @vaibhavrbs 6 лет назад

      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 ,

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

  • @Ram-kv1xp
    @Ram-kv1xp 4 года назад

    @ 5:30 look at the Flicka Da Wrist

  • @rahulkhandelwal46578
    @rahulkhandelwal46578 7 лет назад

    Thank You

  • @SoumikNandi
    @SoumikNandi 4 года назад

    Godly

  • @KnowledgeActionWords
    @KnowledgeActionWords 6 лет назад

    thanks

  • @sachinbhalla14
    @sachinbhalla14 5 лет назад

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

  • @prajitbanerjee8226
    @prajitbanerjee8226 5 лет назад

    nice

  • @sanjaysingh5007
    @sanjaysingh5007 6 лет назад

    sir vector pairs , DSU,

  • @chandangarg1328
    @chandangarg1328 4 года назад +1

    but counting will also take O(n)

  • @SIG442
    @SIG442 7 лет назад

    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.

  • @chaoschao9432
    @chaoschao9432 6 лет назад

    Whether this algorithim is in place?

  • @sumanthv2046
    @sumanthv2046 6 лет назад +1

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

  • @abhishektiwari5932
    @abhishektiwari5932 4 года назад

    print(arr.sort())

  • @kansiram5340
    @kansiram5340 7 лет назад

    sir i need your github link

  • @tusharbhart4105
    @tusharbhart4105 4 года назад +1

    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 ??