there's a small mistake in the dry run in moore's algo at last votes for majority 2 is 1,when majority reaches 3 the votes will be 0, so, majority will be updated in the next iteration so majority will be update as 1, please check, explanation is too good!!!
the dry-run is mainly to understand the simplicity of the code. The exact working code is available in the description too. Mostly you should understand the approach and how you are solving the problem :)
Just not to be confused, Majority Elements are those elements that appears more than half of array length. ie. more than (n/2) times eg. n= 8 So half means 8/2 = 4 [ 2, 2 , 2, 2, 1, 3, 5, 4 ] in this array you may think 2 is majority ,but its not since it appears less than 4 times [ 2, 2 , 2, 3, 2, 2, 5, 4 ] in this case 2 appears five times which is more than 4 times. So majority element is 2
Great video man! Loved this explanation, you are highly underrated. If it's possible can you do python solutions as well for future leetcode videos? Thanks again!
i would advice you to focus on the problem solving method, rather than the language. Trust me...languages will come and go...get your basics right first :)
Shouldn’t there be a line to break out of loop if votes reached more than n/2? So that you don’t have to even loop completely through the array if you have found majority in the first half itself.
the voting method returns 1 for me for array {1, 2, 2, 2, 3, 3, 1 } so is that logic correct? I think after your logic, we need to check again in the array if the count of the majority element is greater than (n/2) to be considered as the majority. In my case, the majority is returned as 1 but 1 is repeated 2 times which is not greater than the expected majority which is (>3). here we can suspect 2 could be the majority but it's not because it is not repeated more than 3 times.
Majority element means the element which occurs more than n/2 times. Your test case is invalid, as it does not have a majority element. What you are talking about is the element occurring maximum number of times.
@@foodandjournieswithme8788 check your test case and code again. Here is the verified output: github.com/nikoo28/java-solutions/blob/master/src/test/java/leetcode/easy/MajorityElementTest.java
@@abhiguptamusic That condition is absolutely correct, whenever votes become =0 you need to update your majority candidate, and then increase the vote count. What error do you see in the condition? Did you try running the code?
Slick & straight! Thank you for breaking the complexity.
You the one of the best here on TH-cam, the way you teach, the way you keep every in a structured manner is super commendable. Subscribed.
thanks for the kind words
Keep making these videos bro, the quality is Amazing!
Fan of you ! Such a clear and crisp explanation
Bro, the quality of your content is exceptional. You deserve more subscribers. Thank you brother.
I wish that too 😄
You've explained in a very simple way!!
After watching 5 videos finally understood ... kaafi acchaa explanation...loved it
Thanks for liking
Beautifully Explained
great solution as always. Thanks alot bhaiya please keep on making such amazing videos.
So nice of you
Superb Explanation!
Thank you 🙂
there's a small mistake in the dry run in moore's algo at last votes for majority 2 is 1,when majority reaches 3 the votes will be 0, so, majority will be updated in the next iteration so majority will be update as 1, please check, explanation is too good!!!
the dry-run is mainly to understand the simplicity of the code. The exact working code is available in the description too. Mostly you should understand the approach and how you are solving the problem :)
Just not to be confused, Majority Elements are those elements that appears more than half of array length.
ie. more than (n/2) times
eg.
n= 8
So half means 8/2 = 4
[ 2, 2 , 2, 2, 1, 3, 5, 4 ] in this array you may think 2 is majority ,but its not since it appears less than 4 times
[ 2, 2 , 2, 3, 2, 2, 5, 4 ] in this case 2 appears five times which is more than 4 times. So majority element is 2
Your teaching is superb. You have a new subscriber.
Thanks a lot 😊
Next level & Awesome explaination with cutest smile. Thank you😊
that is so sweet of you
Glad i found your channel
thank you, your dry run really helped
this was very helpful 😀 Thank you
Awesome explanation 😮😮
Thank you. Very good explanation!
Thanks a lot sir ! Really helpful
SO CLASSY AND please make the playlist of data structures important questions too
playlist: th-cam.com/play/PLFdAYMIVJQHM8Kh5i8P2lGIbJXFPBelRI.html
Thanks for this great explanation
Amazing exaplanation
Best explanation.
Great video, you explained it so well.
Thanks !!
better explanation than Striver .
the second soln was impressive,
pls make a video on peak element ... your videos are so helpful!!!!
Great video man! Loved this explanation, you are highly underrated. If it's possible can you do python solutions as well for future leetcode videos? Thanks again!
i would advice you to focus on the problem solving method, rather than the language. Trust me...languages will come and go...get your basics right first :)
Shouldn’t there be a line to break out of loop if votes reached more than n/2? So that you don’t have to even loop completely through the array if you have found majority in the first half itself.
great explanation
super sir good explanation
Very Well Explained
Thank you so much 🙂
Amazing great explanation
Amazing!
sir in moore algo we take example as 3383372150987216 then 3 is not majority according to that or how to solve that
perfect teacher
perfect student :)
amazing amazing!!!!!
votes suhuld be initialised to 0 at start please correct that or mention that in the description
LEGEND 🖤
Why there are only three types of numbers? In array
you can have as many types.
You're the best👍💯
You are!
what if test case is [1,2,3,4,5]?
it's tough to get an optimized solution 😔.... but I will try to reach it on my own 😊.
sir in optimization in sorting if we have 1234567889 then 8 is not centered how?
I guess because the number of eights must satisfy this condition: "The majority element is the element that appears more than ⌊n / 2⌋".
the voting method returns 1 for me for array {1, 2, 2, 2, 3, 3, 1 } so is that logic correct?
I think after your logic, we need to check again in the array if the count of the majority element is greater than (n/2) to be considered as the majority.
In my case, the majority is returned as 1 but 1 is repeated 2 times which is not greater than the expected majority which is (>3). here we can suspect 2 could be the majority but it's not because it is not repeated more than 3 times.
Majority element means the element which occurs more than n/2 times. Your test case is invalid, as it does not have a majority element.
What you are talking about is the element occurring maximum number of times.
great video
*Sir Please Complete top 150 interview Questions First from Leetcode 🔥*
there are some problems from that list that I have covered...adding new solutions every week :)
@@nikoo28 *Thankyou Sir Loved your teaching Very Clear & Upto the point*
@@nikoo28 thank you!
is moore's algorithm o(1) or 0(n) ?
0(n) time complexity , O(1) - Space complexity
Can we do using 2 pointer
Give me a pseudo code for your approach
What if there is no majority element? How to handle that?
then it will be an entirely different problem. What are you looking to find?
Thanks!
Can we get n/3 solution as well?
do you have a link to the problem?
Not working this code
It gives error when nums=[6,5,5]
What error are you getting? I tried the case again and it gives 5 as the output
@@nikoo28 sir i got output as 6 in the same code
@@foodandjournieswithme8788 check your test case and code again. Here is the verified output: github.com/nikoo28/java-solutions/blob/master/src/test/java/leetcode/easy/MajorityElementTest.java
Thanks
Thanks man, good explaining, if i land a job, ill send you some money 😁
haha..thanks a bunch
For 6,5,5 wrong answer
What answer do you get?
Wrong Code (Wrong understanding of mine)
What part do you think is wrong?
@@nikoo28 first condition vote==0
@@abhiguptamusic That condition is absolutely correct, whenever votes become =0 you need to update your majority candidate, and then increase the vote count. What error do you see in the condition? Did you try running the code?
@@nikoo28 the code is not working with another test cases
@@nikoo28 please try with these test case [1,1,2,3,4]
🔥