Moore voting algorithm

  Рет қаралды 100,365

Techdose

Techdose

4 жыл бұрын

This video explains the most efficient algorithm to find majority element in an array. In this video, i have explained the moore's voting algorithm along with intuition behind the algorithm. First i have explained the algorithm and then explained why the algo will work and finally shown the CODE for it. CODE LINK is present below. 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 :)
CODE LINK: gist.github.com/SuryaPratapK/...
Majority element in an array using BitMasking: • Majority element in an...

Пікірлер: 213
@raghavkakar8092
@raghavkakar8092 4 жыл бұрын
The intuition behind is basically pigeon hole principle, as you'll always have count>=1 (for the majority element) after cancelling with all other elements.
@abhijeetmohansingh4381
@abhijeetmohansingh4381 3 жыл бұрын
yup
@abhijeetmohansingh4381
@abhijeetmohansingh4381 3 жыл бұрын
well that is quite helpful
@sahilamrutagashe1696
@sahilamrutagashe1696 2 жыл бұрын
Thanks brother 😊🙏
@timg8257
@timg8257 8 күн бұрын
I’m new to coding. How would someone attempt this in python in a simple and intuitive example?
@anuragmalhotra2855
@anuragmalhotra2855 4 жыл бұрын
What an exquisite way of explaining such a tough algo.. Keep it up ..
@prabhakarkmr0
@prabhakarkmr0 3 жыл бұрын
you explained so well that i didn't have to look at your code , just listened to you like a story and code it myself and it got submitted in 2 go.thanks a lot for the clearity.Peace.
@techdose4u
@techdose4u 3 жыл бұрын
Nice :)
@jlecampana
@jlecampana 2 жыл бұрын
Great video! This problem just recently appeared in a Google Phone Screen, it's a very useful algorithm to have in one's toolkit!
@SolutionHunterz
@SolutionHunterz 4 жыл бұрын
Thanks for the awesome explanation!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome bro :)
@mohamedsayed8697
@mohamedsayed8697 Жыл бұрын
Thanks, this is really helpful.
@annas8308
@annas8308 4 жыл бұрын
Your teaching is great!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@GAURAVJEETSINGH1
@GAURAVJEETSINGH1 2 жыл бұрын
Amazing explanation!
@vasushrivastava4578
@vasushrivastava4578 2 жыл бұрын
the explanation is just perfect. thanks :p
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@rahuldeshpande3516
@rahuldeshpande3516 6 ай бұрын
Its simple yet incredibly genius
@samjason324
@samjason324 3 жыл бұрын
Amazing, I understand the algorithm after watching first 3 minutes of the video.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises.
@mj-sv7ep
@mj-sv7ep 3 жыл бұрын
Thank you, this helped me understand the algo. A minor fix at 1:30 - we should refer to the starting element as zeroth element and not as first element.
@Idanh
@Idanh 3 жыл бұрын
Hi, Thanks for this great explanation. That being said, I think that the code you suggest in findCandidate has a "redundant part" (yet it's correct). Inside the for loop, you first check if the current element equals to the majority element to increase or decrease the counter, and afterwards check the counter for zero to assign new potential candidate. Suppose we have 3 3 2 4 6, then in your code 4 element becomes a candidate despite there's no reason for it (4 isn't different from 2 for that manner). Only the next element should be considered as potential majority element. As I said it doesn't effect correctness, but yet I think it making more sense this way (and also wiki supports this). The code inside the for loop should be: if (count == 0) { ... (as it now) } else { if (a[maj_index) != a[i]) ... (as it now) }
@techdose4u
@techdose4u 3 жыл бұрын
Need to check it :) Thanks for your suggestion
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@joseph2073
@joseph2073 2 жыл бұрын
@@freshcontent3729 hey did u find a solution to this ? what should u say ?
@yashgoswami5374
@yashgoswami5374 Жыл бұрын
@@freshcontent3729 I'm thinking that who ever is getting placed , they must know all the solutions of the problems which are beeing asked, because It's' nearly impossible to come up with such soln untill you haven't atleast go through it once
@krishnendumandal6547
@krishnendumandal6547 10 ай бұрын
This point is important Because when you will try to solve its extended version (leetcode 229) the approach in the video fall apart. You need to assign the new value only on count==0 to make it work.
@doge-coin
@doge-coin 4 жыл бұрын
Nice video, thank you sir!!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@priyachaturvedi9381
@priyachaturvedi9381 3 жыл бұрын
Awesome explanation!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@AmanKumar-xw5kl
@AmanKumar-xw5kl 4 жыл бұрын
simply awesome.. you are doing great job sir..
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@harshpanwar1550
@harshpanwar1550 3 жыл бұрын
Thanks a lot!!!!! That was amazinggggg
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@martinharris4416
@martinharris4416 3 жыл бұрын
Im becoming fan of your explanations
@techdose4u
@techdose4u 3 жыл бұрын
Woww ❤️
@samagrashukla
@samagrashukla 4 жыл бұрын
good explaination, thanks!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@ahnissingh6911
@ahnissingh6911 25 күн бұрын
Smooth and clear
@shaurya478
@shaurya478 4 жыл бұрын
Very nice explanation... Thanks
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@nikhitham4815
@nikhitham4815 2 ай бұрын
Thank you!
@tsupreetsingh
@tsupreetsingh 4 жыл бұрын
Good explanation
@andiamakaza24
@andiamakaza24 2 жыл бұрын
Nice explanation sir
@DiaryOfMuhib
@DiaryOfMuhib 4 жыл бұрын
Awesome explanation bro
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@nskkknagajayanth1199
@nskkknagajayanth1199 Жыл бұрын
Super Explanation 😃
@techdose4u
@techdose4u Жыл бұрын
Thanks :)
@riteshsaha6881
@riteshsaha6881 2 жыл бұрын
This is so clever.
@arthamsreenivas8858
@arthamsreenivas8858 4 жыл бұрын
nice video and good explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@arpitmani6553
@arpitmani6553 3 жыл бұрын
if we need to find elements which appears more than N/k where 0
@techdose4u
@techdose4u 3 жыл бұрын
You can see this algorithm on stack overflow. For N/K.
@arpitmani6553
@arpitmani6553 3 жыл бұрын
@@techdose4u OK.....THANKS SIR
@APragmaticProgrammer-yl5pm
@APragmaticProgrammer-yl5pm Жыл бұрын
great video
@sarthakbhatia7639
@sarthakbhatia7639 4 жыл бұрын
So this is my first video on your channel...Nice explanation!...Subscribed
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@dhruvagarwal882
@dhruvagarwal882 4 жыл бұрын
very nice explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@dorisli3461
@dorisli3461 4 жыл бұрын
omg best one i've seen on Moore's voting algorithm!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@harshavardhan2775
@harshavardhan2775 2 жыл бұрын
Neat explanation.
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@kongzilla2897
@kongzilla2897 3 жыл бұрын
Nice Thanks :)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@code7434
@code7434 4 жыл бұрын
Amazing Man
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@code7434
@code7434 4 жыл бұрын
@@techdose4u Please add more interview problems
@devanshgoel3433
@devanshgoel3433 Жыл бұрын
thank u sir
@xixnecroxix
@xixnecroxix 3 жыл бұрын
very good explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises.
@priyanshigupta1359
@priyanshigupta1359 3 жыл бұрын
very nice expalanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@gouthamsidhardha681
@gouthamsidhardha681 4 жыл бұрын
that was nice xD
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@neerajmahapatra5239
@neerajmahapatra5239 2 жыл бұрын
Understood❤
@rajeevcbhatia
@rajeevcbhatia 4 жыл бұрын
Thank you for the explanation! I have a question. Will it work for this array? [3,2,3] my understanding is that the steps will be this way:- 1. index 0, majority = 3, count = 1 2. index 1, element != majority so count--, count is 0 now so new majority is 2 and count is 1 3. index 2, element != majority so count--, count is 0 now so new majority is 3 and count is 1 at the end the count is not greater than n/2, which is 1 so this is not considered the majority. am i missing something here? Would appreciate the clarity :)
@techdose4u
@techdose4u 4 жыл бұрын
Yes....you are long at last step. Doesn't matter what the count value is, the element in majority variable is the candidate for being majority element if any. So the next step is to verify by counting the frequency of leftover element in majority that is 3 here. You see it occurs 2 times and so it is majority. So this is a 2 times traversal process.
@rajeevcbhatia
@rajeevcbhatia 4 жыл бұрын
@@techdose4u got it, thank you!
@apoorvamittal1379
@apoorvamittal1379 3 жыл бұрын
When count becomes 0, why do we make the current element as the majority element? For example, at 3 the count becomes 0, so the majority element should be updated to the next element (4) and then the count should be incremented to 1? Does this not make any difference?
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@siddharthkalani7535
@siddharthkalani7535 3 жыл бұрын
i see your explanation and seeing the comments below people do understand, but I didn't get it. ho wit works in [1,2,1,2,1] or [3,2,3,3,4]
@techdose4u
@techdose4u 3 жыл бұрын
2 cancels 2 again 2 cancels 1 and finally candidate left is only 1. Now we again traverse the array and check if the candidate 1 is occuring for more than N/2 times. If yes then 1 is majority element otherwise there is no majority element.
@dheerjain2884
@dheerjain2884 3 жыл бұрын
@TECH DOSE in an interview, Do we name the algo when we explain the approach to the interviewer? Like in this , its a Moore voting algorithm.
@techdose4u
@techdose4u 3 жыл бұрын
No. Avoid naming algos.
@dheerjain2884
@dheerjain2884 3 жыл бұрын
@@techdose4u Thanks for such a speedy reply. So when we explain the algo and then the intuition behind it, Wont the interview have some suspicion as these algos cannot strike at the moment. You need to know the working beforehand.
@techdose4u
@techdose4u 3 жыл бұрын
He will obviously be knowing it. If he asks the name then tell him otherwise you shouldn't because he might ask some related questions which you won't be knowing.
@dheerjain2884
@dheerjain2884 3 жыл бұрын
@@techdose4u True that. Thanks!!!
@techdose4u
@techdose4u 3 жыл бұрын
@@dheerjain2884 welcome
@varunoyal87
@varunoyal87 3 жыл бұрын
Thanks for the explanation , I have one query in between 5:10 to 5:30 in worst case of 2,3,7,3,4 ME returns 4 as ME but its not so how we should handle this case I did not understand. Can you please explain a little more. Thanks alot
@techdose4u
@techdose4u 3 жыл бұрын
As I said, if you have a majority element then you will get correct result in ME else not. So, whatever you get in ME needs to be verified by counting the occurence by traversing the array again.
@varunoyal87
@varunoyal87 3 жыл бұрын
@@techdose4u okay thanks . so there is a need to traverse again to verified the element.
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain broo but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@AmirAli-gv7kn
@AmirAli-gv7kn 2 жыл бұрын
@@varunoyal87 no, you don't have to traverse it again. Since, you already have a counter variable alongside. And 3 also does not satisfy majority element criteria which is greater than n/2 (in case of 5 element the majority element has to appear more than 2 times) which didn't happen in the above example
@dmitriy_dokshin
@dmitriy_dokshin 2 жыл бұрын
You don't need to set element when count becomes 0, if current value is the majority element next iteration will fix that.
@suraj_meena_505
@suraj_meena_505 3 жыл бұрын
For me this video actually began at 3:29 "so why this technique is actually working ?" though I was not so convinced with later part
@techdose4u
@techdose4u 3 жыл бұрын
It works because if we have majority element then if we try to cancel one element with another different element, no matter in what order, the majority element will remain. The first pass only finds the candidate for being a majority element and then we verify in the second pass if the candidate is actually the majority element. Take examples to understand clearly.
@suraj_meena_505
@suraj_meena_505 3 жыл бұрын
@@techdose4u thanks for reply...I got it now
@varunnarayanan8720
@varunnarayanan8720 3 жыл бұрын
@@techdose4u Yes this one line explains the why very well. Thanks.
@abhishekks6782
@abhishekks6782 4 жыл бұрын
What is the time complexity and space complexity of this code?
@thewatcherlollol
@thewatcherlollol 4 жыл бұрын
O(N) time complexity and O(1) space complexity.
@godlevelchoker4543
@godlevelchoker4543 Жыл бұрын
Tip: more's voting algorithm + mapping , time complexity will be better than anyone
@techdose4u
@techdose4u Жыл бұрын
👍🏽
@godlevelchoker4543
@godlevelchoker4543 Жыл бұрын
​@@techdose4u i appreciate your video , thank you bro
@a036nikhilsannat4
@a036nikhilsannat4 2 жыл бұрын
How to prove this algorithm?
@AdityaLSharma
@AdityaLSharma 2 жыл бұрын
Wondering how many years would it take for me to explain concept with such clarity lol
@sachinnegi8814
@sachinnegi8814 3 жыл бұрын
What if we sort the array and get the middle element.
@techdose4u
@techdose4u 3 жыл бұрын
You can do that. But it is not the most efficient process and also if the array is immutable, you can't do that.
@SR-we1vl
@SR-we1vl 4 жыл бұрын
@TECH DOSE Does the relation is greater than N/2 or greater than and equal to N/2 elements!?
@techdose4u
@techdose4u 4 жыл бұрын
Greater than N/2
@SR-we1vl
@SR-we1vl 4 жыл бұрын
@@techdose4u Thanks!
@AJAYPAL-zu6lg
@AJAYPAL-zu6lg 2 жыл бұрын
{2,1,2,2,2,3} n=6 and candidate element. Is 3 but 2 is a ME which present
@arpit39agarwal
@arpit39agarwal 3 жыл бұрын
So in this case you have taken {1,3,3,1,2} so no majority element right ?
@techdose4u
@techdose4u 3 жыл бұрын
Yes right
@tejitha6762
@tejitha6762 3 жыл бұрын
If the question is of n/3 and the array is[1,1,1,2,3,4,7] then ?
@techdose4u
@techdose4u 3 жыл бұрын
This problem is not solved using this algo. There is a generalized algo for N/K which is very well explained on stackoverflow. I will explain that in future.
@namanagrawal4868
@namanagrawal4868 2 жыл бұрын
@@techdose4u please make a video on that as well
@nikhilpandey3486
@nikhilpandey3486 2 жыл бұрын
@@techdose4u But then it is giving wrong ans
@kirtigera2225
@kirtigera2225 4 жыл бұрын
Sir please make video on segment tree and lazy propagation
@techdose4u
@techdose4u 4 жыл бұрын
Many people are requesting the same. I guess i have no choice now but to make it. Will upload it surely in coming month. I have added it in todo list.
@mohitlalwani3505
@mohitlalwani3505 4 жыл бұрын
is this algo work for N/3 repeat.
@techdose4u
@techdose4u 4 жыл бұрын
Nope. It works for Freq > N/2
@sed6845
@sed6845 4 жыл бұрын
Hi, can you tell me why the majority element is a problem ?? what will happen if we have majority element in an array ??
@techdose4u
@techdose4u 4 жыл бұрын
Good question. Let's say there are many contestents in a pool, standing for the same postion. Then people might VOTE for anyone. Now let's assume the sequence of VOTEs casted by people forms an array. Now, your criteria to select the winning person is by majority VOTE. So, you will find the majority element in the array. This is one simple example.
@sed6845
@sed6845 4 жыл бұрын
@@techdose4u thank you very much. :)))) I got it now.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@abhishekchoudhary8023
@abhishekchoudhary8023 3 ай бұрын
2 pointer se bhi ho jayega
@areshbabu
@areshbabu 2 жыл бұрын
Does this algorithm works for the following array {1, 1, 1, 2, 3, 5, 7} Count becomes zero for the majority element 1 by end of the array. And once the count becomes zero we change the majority element to 7 and return it. In the next step, if you find the frequency of 7, then its less than n/2 times and we return the output as no majority element. That is incorrect output. Please correct me if my understanding is wrong.
@tushargahlaut5812
@tushargahlaut5812 2 жыл бұрын
For Majority Elements, the count should be greater than N/2, like if N=7, then N/2=3 (integer), so your majority element (let's say 1) should occur at least 4 times, so the algorithm is correct.
@areshbabu
@areshbabu 2 жыл бұрын
Thanks @Tushar Gahlaut for giving the clarity. I was trying for n/3 scenario which may not work in this case.
@evantilley
@evantilley 3 жыл бұрын
Thank you! Was wondering why the complexity is 2*N? I get we have to traverse it once since we're looping through the array, but where is the second traversal coming from?
@techdose4u
@techdose4u 3 жыл бұрын
1st traversal is to find the candidate. 2nd traversal is to verify if candidate is majority element.
@evantilley
@evantilley 3 жыл бұрын
@@techdose4u ah okay thank you!
@jasdfpasdfakdsjhfa
@jasdfpasdfakdsjhfa 2 жыл бұрын
@@techdose4u Oh gotcha, but this is to verify only right? For example in a question on leetcode, they guarantee that the array passed does contain a majority element, therefore we only need to return the candidate.
@aayush5474
@aayush5474 4 жыл бұрын
this wont work if ME is > n/3 right?
@techdose4u
@techdose4u 4 жыл бұрын
Yes. This is only for >N/2.
@abhireddy8164
@abhireddy8164 4 жыл бұрын
Sir please make a video on dice throw problem sir
@techdose4u
@techdose4u 4 жыл бұрын
I have queued your problem in to-do list. Will upload it in Feb.
@nikhilpandey3486
@nikhilpandey3486 2 жыл бұрын
It fails for the case(the condition is greater than n/3) 1, 1, 1, 2, 3, 5, 7 Here the program return -1 but the ans is 1
@ayusharyan683
@ayusharyan683 2 жыл бұрын
total element in the array is 7. Acc to the algo. count of 1 must be greater than n/2 i.e 3 than it'll be answer. But here count of 1 is not greater than 3. So answer is -1.
@user-nf2br1mu6o
@user-nf2br1mu6o 2 жыл бұрын
What if there's more than one majority element?
@aisharawat9102
@aisharawat9102 2 жыл бұрын
Do elements ki Freq n/2 se zaada nhi ho sktii....vo to greater than 2(n/2) ho jaaega means greater than n (size of the array)
@babitakumari2315
@babitakumari2315 4 жыл бұрын
candidate element will always be the one that occcurs maximum no of times ???
@techdose4u
@techdose4u 4 жыл бұрын
No that is not tru....consider 1,1,1,2,3,4,5. Here 1 occurs the most times but candidate will be 5. If there is a majority element then candidate will be the majority element for sure otherwise any random element can become the candidate (probably from the ones occuring near the right end).
@babitakumari2315
@babitakumari2315 4 жыл бұрын
@@techdose4u thanks
@SurajRaj-vh5ei
@SurajRaj-vh5ei 4 жыл бұрын
1, 2, 3, 4, 1, 6, 1, 7, 1, 7 For the above input. I don't thik this will work. It will return 7 as majorelement. But it should be 1. Can you please explain the above input that if I'm missing something.
@techdose4u
@techdose4u 4 жыл бұрын
There is no majority element. After finding candidate element, you need to traverse again and find the count of that element in the array and see if it occurs for more than N/2 times. Then only it will be a majority element.
@SurajRaj-vh5ei
@SurajRaj-vh5ei 4 жыл бұрын
Thanks. But for this input, candidate element will be 7 by the provided algorithm, not 1. So the function will return -1 as output but it should be 1. As 1 is repeated 4 times.
@MrAkshat7
@MrAkshat7 4 жыл бұрын
@@SurajRaj-vh5ei Bro, this array doesn't have a majority element. 1 occurs 4 times but the definition of majority element is that the element which occurs more than floor(n/2) times (which is 5 for your array) is the ME. So the algorithm is valid.
@aishiksarkar8226
@aishiksarkar8226 3 жыл бұрын
What if two 3s were not consecutive?
@techdose4u
@techdose4u 3 жыл бұрын
It wouldn't matter
@abhinavgarg5611
@abhinavgarg5611 3 жыл бұрын
Simple Explanation (Majority Element occurs in following 2 cases only) 1. Element occurs consecutively for more than n/2 times 2. Element occurs non-consecutively for more than n/2 times In either of above 2 cases i.e. if we assume that frequency =n/2 +k , (k>=1) the remaining n/2-k , (k>=1) elements won't be able to cancel the frequency of majority element. Hence we always get a majority element.
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@easward
@easward 4 жыл бұрын
What if the array is 2 3 2 4 2 ?
@watcher5232
@watcher5232 4 жыл бұрын
Works fine. 2 is the majority element and its frequency is > n/2
@gopinath7846
@gopinath7846 3 жыл бұрын
can u possible try to solve this probelm "tomorrow" to "ooorrtmw"
@techdose4u
@techdose4u 3 жыл бұрын
??
@gopinath7846
@gopinath7846 3 жыл бұрын
@@techdose4u it say that o was 3 times times repeated r is 2 times repeated in the way we want to show the output like
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@dheer jain bro but what if the interveiwer asks .. "tell honestly.. u knew the algo from before right ? then why did you act like you didnt? " .. then in this case what do we say if such a situatation or a smiliar one arises. Becacuse it's obivious ki one cant come up with such a solution so fast in the interview moment
@manojpokhriyal1183
@manojpokhriyal1183 2 жыл бұрын
Please take the case [2,2,2,2,4,2,5,3]...the should be 2 but according to this algo if we go..we will get count =4 upto fourth 2 , then 2 not equal to 4 count=3 , then 4 !=2 , count=2...2!=5 count=1 , ....5!=3 count =0...when count become 0 we update majority element by 3 and make count =1 ... So it will verify for 3 not 2 ...please clear this doubt.
@abhaytiwari6411
@abhaytiwari6411 4 жыл бұрын
fabulus video make video on this type of topics
@techdose4u
@techdose4u 4 жыл бұрын
Keep suggesting topics :)
@abhaytiwari6411
@abhaytiwari6411 4 жыл бұрын
OK Bro
@amanrubey
@amanrubey 4 жыл бұрын
better than gfg!
@soneshengg
@soneshengg 3 жыл бұрын
Thanks!
@dheerajpoonia2175
@dheerajpoonia2175 4 жыл бұрын
1 3 2 3 5 3 4 5 3 2 3 2 3 5 Sir algorithm not working for these test cases why ?
@techdose4u
@techdose4u 4 жыл бұрын
Its working perfectly fine bro. Majority element is always greater than N/2. In both test cases, you don't have majority element. Please rewatch the video carefully.
@dheerajpoonia2175
@dheerajpoonia2175 4 жыл бұрын
But sir in second test case 3 is occurring 3 time so 3>=6/2
@dheerajpoonia2175
@dheerajpoonia2175 4 жыл бұрын
And my other doubt is that "Moore voting algorithm is work when n/2 is not given and we have to find majority elements from complete array"
@techdose4u
@techdose4u 4 жыл бұрын
@@dheerajpoonia2175 but majority means > N/2. Haven't I told this in the video?
@techdose4u
@techdose4u 4 жыл бұрын
@@dheerajpoonia2175 we dont need to find for N/2 and that's why it is an algo.
@bajisyed9532
@bajisyed9532 2 жыл бұрын
This is failing for the input {2,2,2,3,4,4}
@ijaz2020
@ijaz2020 4 жыл бұрын
847. Shortest Path Visiting All Nodes this pblm pls
@techdose4u
@techdose4u 4 жыл бұрын
Can you please provide a link explaining the problem statement.
@ijaz2020
@ijaz2020 4 жыл бұрын
@@techdose4u leetcode.com/problems/shortest-path-visiting-all-nodes/
@techdose4u
@techdose4u 4 жыл бұрын
Queued 👍
@Shivam_Manswalia
@Shivam_Manswalia 4 жыл бұрын
python: class Solution: def majorityElement(self, nums: List[int]) -> int: nums.sort() return nums[len(nums)//2]
@techdose4u
@techdose4u 4 жыл бұрын
Thanks for sharing
@techdose4u
@techdose4u 4 жыл бұрын
Correct
@abhishekmerotha9928
@abhishekmerotha9928 3 жыл бұрын
what about 1 2 1 3
@techdose4u
@techdose4u 3 жыл бұрын
Nothing is Majority in this case.
@sandeepkumawat4982
@sandeepkumawat4982 4 жыл бұрын
Please make a video on n/3 repeat element🙏🙏
@techdose4u
@techdose4u 4 жыл бұрын
Good question. I will try as soon as I get time.
@nikhil.pandey
@nikhil.pandey 4 жыл бұрын
@@techdose4u please make it sir!
@j.y.
@j.y. Жыл бұрын
azyum are is sorted fost
@easward
@easward 4 жыл бұрын
Simpler way is If element at first and last are not same then an element has to occur consecutively else there is no such majority element.
@tashifhoda2414
@tashifhoda2414 Жыл бұрын
Another way to visualize why this algorithm works is to think of an array of any size (say 6). Now try to fill it in with an element such that this element becomes the majority element. For our example a number x has to appear at least 4 times in the array to be able to satisfy the criteria of being a majority element. You will see that there is no way you can fill in the the number without it occuring consecutively, at least once in the array. This very fact is what makes this algorithm work.
@MrJiJoJu
@MrJiJoJu Жыл бұрын
No need for the number to occur consecutively, for example in 13131, 1 is majority but there no two consecutive 1.
@tashifhoda2414
@tashifhoda2414 Жыл бұрын
@@MrJiJoJu 1 is not the majority element in your example because it appears exactly n/2 times (3 times). It has to appear 4 times to be the majority element.
@MrJiJoJu
@MrJiJoJu Жыл бұрын
@@tashifhoda2414 In my example there are 5 elements, and 1 appears 3 times (3 > 5/2)..3 is a majority in an array of 5 elements
@tashifhoda2414
@tashifhoda2414 Жыл бұрын
@@MrJiJoJu Ohh, my bad then. This is a very valid counterexample. Thanks. Looks like I need to update my intuition ^_^
@igaming_
@igaming_ 2 жыл бұрын
This fails for 2,3,2,4,2
@iamnotahuman2172
@iamnotahuman2172 4 ай бұрын
No it does not
@saravanansivakumar9259
@saravanansivakumar9259 4 ай бұрын
2 is the majority candidate at last with count 1. So 2 is the correct answer and it does
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 95 М.
Majority element in an array | Bitmasking
11:25
Techdose
Рет қаралды 16 М.
Задержи дыхание дольше всех!
00:42
Аришнев
Рет қаралды 3,8 МЛН
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 26 МЛН
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 8 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 12 МЛН
ADS1: Boyer-Moore basics
8:50
Ben Langmead
Рет қаралды 252 М.
The LeetCode Fallacy
6:08
NeetCode
Рет қаралды 462 М.
Boyer Moore Majority Vote Algorithm
5:52
AlgosWithMichael
Рет қаралды 17 М.
Largest number formed from an array
8:33
Techdose
Рет қаралды 117 М.
Closest Pair of Points (Divide and Conquer) Explained
8:45
Single element in a sorted array | Leetcode #540
8:26
Techdose
Рет қаралды 53 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
Dutch National Flag Algorithm. Explained with playing cards.
12:11
Boyer Moore Horspool Algorithm
6:40
Mike Slade
Рет қаралды 162 М.
Задержи дыхание дольше всех!
00:42
Аришнев
Рет қаралды 3,8 МЛН