Majority Element (LeetCode 169) | Full solution with 4 different methods | Interview Essential

  Рет қаралды 17,180

Nikhil Lohia

Nikhil Lohia

Күн бұрын

A majority candidate is defined as more than 50%. So in a given array of integers you have different methods to find it. Watch the video to learn 4 ways how you can start and ultimately arrive at an efficient solution. This video provides a perfect approach to attack this problem step by step with animations and visuals.
Actual problem on LeetCode: leetcode.com/problems/majorit...
Chapters:
00:00 - Intro
00:51 - Problem Statement and Description
02:48 - Brute Force Solution
04:27 - Solution by Sorting
07:14 - Using a HashTable
09:35 - Moore’s Voting Algorithm O(1) space
12:14 - Dry-run of Code
13:47 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Algorithms: • Brute Force algorithms...
LeetCode Problems: • Leetcode Solutions
Other easy difficulty problems: • Easy Problems
Dynamic Programming: • Dynamic Programming ea...
Quick Sort: • Quick Sort super easy ...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview

Пікірлер: 79
@michaelagedie9433
@michaelagedie9433 5 ай бұрын
Keep making these videos bro, the quality is Amazing!
@ruthikapamar7981
@ruthikapamar7981 5 ай бұрын
Slick & straight! Thank you for breaking the complexity.
@Rits2203
@Rits2203 Жыл бұрын
You've explained in a very simple way!!
@velocity1186
@velocity1186 Күн бұрын
Your teaching is superb. You have a new subscriber.
@sheldoncooper7990
@sheldoncooper7990 9 ай бұрын
You the one of the best here on KZbin, the way you teach, the way you keep every in a structured manner is super commendable. Subscribed.
@nikoo28
@nikoo28 9 ай бұрын
thanks for the kind words
@ayushijindal4898
@ayushijindal4898 Жыл бұрын
Beautifully Explained
@marcelocarvalholopes
@marcelocarvalholopes 5 ай бұрын
Thank you. Very good explanation!
@kunalkheeva
@kunalkheeva Жыл бұрын
thank you, your dry run really helped
@user-gy7hz5tn1w
@user-gy7hz5tn1w 6 ай бұрын
this was very helpful 😀 Thank you
@RamuSriram0
@RamuSriram0 5 ай бұрын
Bro, the quality of your content is exceptional. You deserve more subscribers. Thank you brother.
@nikoo28
@nikoo28 4 ай бұрын
I wish that too 😄
@amanverma8258
@amanverma8258 11 ай бұрын
Thanks a lot sir ! Really helpful
@riddle_cicad007
@riddle_cicad007 4 ай бұрын
Great video, you explained it so well. Thanks !!
@SandipKumarRoy12
@SandipKumarRoy12 10 ай бұрын
Awesome explanation 😮😮
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
great solution as always. Thanks alot bhaiya please keep on making such amazing videos.
@nikoo28
@nikoo28 Жыл бұрын
So nice of you
@m.abrarsheikh9865
@m.abrarsheikh9865 Ай бұрын
Next level & Awesome explaination with cutest smile. Thank you😊
@nikoo28
@nikoo28 20 күн бұрын
that is so sweet of you
@negxativexd2622
@negxativexd2622 6 ай бұрын
After watching 5 videos finally understood ... kaafi acchaa explanation...loved it
@nikoo28
@nikoo28 5 ай бұрын
Thanks for liking
@MythBuster28_10
@MythBuster28_10 4 ай бұрын
Glad i found your channel
@hameedmulani21
@hameedmulani21 Жыл бұрын
Superb Explanation!
@nikoo28
@nikoo28 Жыл бұрын
Thank you 🙂
@Hello-l3i
@Hello-l3i Күн бұрын
pls make a video on peak element ... your videos are so helpful!!!!
@CelestialEditzHub
@CelestialEditzHub 11 ай бұрын
Amazing great explanation
@shubhammanecr7
@shubhammanecr7 11 ай бұрын
Amazing!
@user-jr1vc3cc6c
@user-jr1vc3cc6c Жыл бұрын
amazing amazing!!!!!
@ajaykumar-yk7to
@ajaykumar-yk7to Жыл бұрын
super sir good explanation
@bhumikabansal6022
@bhumikabansal6022 10 ай бұрын
SO CLASSY AND please make the playlist of data structures important questions too
@nikoo28
@nikoo28 9 ай бұрын
playlist: kzbin.info/aero/PLFdAYMIVJQHM8Kh5i8P2lGIbJXFPBelRI
@CSstudent_1001
@CSstudent_1001 9 ай бұрын
LEGEND 🖤
@tarunbhatt5003
@tarunbhatt5003 Жыл бұрын
great video
@ahmedbaig8543
@ahmedbaig8543 6 ай бұрын
Very Well Explained
@nikoo28
@nikoo28 6 ай бұрын
Thank you so much 🙂
@mehbubrohit12
@mehbubrohit12 9 ай бұрын
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!
@nikoo28
@nikoo28 8 ай бұрын
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 :)
@funnymoment9164
@funnymoment9164 9 ай бұрын
Thanks!
@tng3100
@tng3100 4 ай бұрын
the second soln was impressive,
@hameedmulani21
@hameedmulani21 Жыл бұрын
You're the best👍💯
@nikoo28
@nikoo28 Жыл бұрын
You are!
@LalitSingh-nd5vx
@LalitSingh-nd5vx 4 ай бұрын
better explanation than Striver .
@subee128
@subee128 6 ай бұрын
Thanks
@arnavkukreti2009
@arnavkukreti2009 10 ай бұрын
perfect teacher
@nikoo28
@nikoo28 9 ай бұрын
perfect student :)
@palanivelraju1981
@palanivelraju1981 Жыл бұрын
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!!!
@nikoo28
@nikoo28 Жыл бұрын
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 :)
@yash_14h
@yash_14h 10 ай бұрын
Why there are only three types of numbers? In array
@nikoo28
@nikoo28 9 ай бұрын
you can have as many types.
@dineshkinibailoor340
@dineshkinibailoor340 6 ай бұрын
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.
@nikoo28
@nikoo28 6 ай бұрын
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.
@abhishekchoudhary8023
@abhishekchoudhary8023 2 ай бұрын
Can we do using 2 pointer
@nikoo28
@nikoo28 2 ай бұрын
Give me a pseudo code for your approach
@mdsalik9374
@mdsalik9374 8 ай бұрын
Can we get n/3 solution as well?
@nikoo28
@nikoo28 7 ай бұрын
do you have a link to the problem?
@mdsalik9374
@mdsalik9374 8 ай бұрын
What if there is no majority element? How to handle that?
@nikoo28
@nikoo28 7 ай бұрын
then it will be an entirely different problem. What are you looking to find?
@enriquegrageda
@enriquegrageda 5 ай бұрын
Thanks man, good explaining, if i land a job, ill send you some money 😁
@nikoo28
@nikoo28 5 ай бұрын
haha..thanks a bunch
@SMARTRIXDbztttmods
@SMARTRIXDbztttmods Жыл бұрын
*Sir Please Complete top 150 interview Questions First from Leetcode 🔥*
@nikoo28
@nikoo28 Жыл бұрын
there are some problems from that list that I have covered...adding new solutions every week :)
@SMARTRIXDbztttmods
@SMARTRIXDbztttmods Жыл бұрын
@@nikoo28 *Thankyou Sir Loved your teaching Very Clear & Upto the point*
@hameedmulani21
@hameedmulani21 Жыл бұрын
​@@nikoo28 thank you!
@foodandjournieswithme8788
@foodandjournieswithme8788 3 ай бұрын
It gives error when nums=[6,5,5]
@nikoo28
@nikoo28 3 ай бұрын
What error are you getting? I tried the case again and it gives 5 as the output
@foodandjournieswithme8788
@foodandjournieswithme8788 3 ай бұрын
@@nikoo28 sir i got output as 6 in the same code
@nikoo28
@nikoo28 3 ай бұрын
@@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
@MeghnaMukesh-hd5zk
@MeghnaMukesh-hd5zk Ай бұрын
The mistake in the code is that the loop starts with i = 0, which causes the initial element to be counted twice. Specifically, when the loop starts, nums[0] is already assigned to the majority and the vote is set to 1. The loop then starts from i = 0, incrementing votes for the same element. The correct approach is to start the loop from i = 1. for (int i = 1; i < nums.length; i++)
@abhiguptamusic
@abhiguptamusic Жыл бұрын
Wrong Code (Wrong understanding of mine)
@nikoo28
@nikoo28 Жыл бұрын
What part do you think is wrong?
@abhiguptamusic
@abhiguptamusic Жыл бұрын
@@nikoo28 first condition vote==0
@nikoo28
@nikoo28 Жыл бұрын
@@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?
@abhiguptamusic
@abhiguptamusic Жыл бұрын
@@nikoo28 the code is not working with another test cases
@abhiguptamusic
@abhiguptamusic Жыл бұрын
@@nikoo28 please try with these test case [1,1,2,3,4]
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 93 М.
🤔Какой Орган самый длинный ? #shorts
00:42
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 8 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 57 МЛН
New model rc bird unboxing and testing
00:10
Ruhul Shorts
Рет қаралды 23 МЛН
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 89 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 623 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 431 М.
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 408 М.
🤔Какой Орган самый длинный ? #shorts
00:42