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

Поделиться
HTML-код
  • Опубликовано: 30 июл 2024
  • 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

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

  • @ruthikapamar7981
    @ruthikapamar7981 5 месяцев назад +7

    Slick & straight! Thank you for breaking the complexity.

  • @michaelagedie9433
    @michaelagedie9433 5 месяцев назад +3

    Keep making these videos bro, the quality is Amazing!

  • @Rits2203
    @Rits2203 Год назад +1

    You've explained in a very simple way!!

  • @sheldoncooper7990
    @sheldoncooper7990 9 месяцев назад +3

    You the one of the best here on RUclips, 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

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

    thank you, your dry run really helped

  • @riddle_cicad007
    @riddle_cicad007 5 месяцев назад

    Great video, you explained it so well.
    Thanks !!

  • @marcelocarvalholopes
    @marcelocarvalholopes 5 месяцев назад

    Thank you. Very good explanation!

  • @amanverma8258
    @amanverma8258 11 месяцев назад +1

    Thanks a lot sir ! Really helpful

  • @ayushijindal4898
    @ayushijindal4898 Год назад +1

    Beautifully Explained

  • @RamuSriram0
    @RamuSriram0 5 месяцев назад

    Bro, the quality of your content is exceptional. You deserve more subscribers. Thank you brother.

    • @nikoo28
      @nikoo28  4 месяца назад +1

      I wish that too 😄

  • @user-gy7hz5tn1w
    @user-gy7hz5tn1w 6 месяцев назад +1

    this was very helpful 😀 Thank you

  • @satyamgupta6030
    @satyamgupta6030 Год назад +1

    great solution as always. Thanks alot bhaiya please keep on making such amazing videos.

  • @akhintheruvath
    @akhintheruvath День назад

    Thanks for this great explanation

  • @SandipKumarRoy12
    @SandipKumarRoy12 10 месяцев назад +1

    Awesome explanation 😮😮

  • @negxativexd2622
    @negxativexd2622 6 месяцев назад

    After watching 5 videos finally understood ... kaafi acchaa explanation...loved it

    • @nikoo28
      @nikoo28  6 месяцев назад

      Thanks for liking

  • @rakeshbabu3839
    @rakeshbabu3839 4 часа назад

    Amazing exaplanation

  • @shubhammanecr7
    @shubhammanecr7 11 месяцев назад +1

    Amazing!

  • @m.abrarsheikh9865
    @m.abrarsheikh9865 Месяц назад

    Next level & Awesome explaination with cutest smile. Thank you😊

    • @nikoo28
      @nikoo28  29 дней назад

      that is so sweet of you

  • @CelestialEditzHub
    @CelestialEditzHub 11 месяцев назад

    Amazing great explanation

  • @user-jr1vc3cc6c
    @user-jr1vc3cc6c Год назад +1

    amazing amazing!!!!!

  • @MythBuster28_10
    @MythBuster28_10 4 месяца назад

    Glad i found your channel

  • @arnavkukreti2009
    @arnavkukreti2009 День назад

    great explanation

  • @Hello-l3i
    @Hello-l3i 10 дней назад

    pls make a video on peak element ... your videos are so helpful!!!!

  • @hameedmulani21
    @hameedmulani21 Год назад +1

    Superb Explanation!

  • @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 месяцев назад +4

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

  • @CSstudent_1001
    @CSstudent_1001 9 месяцев назад

    LEGEND 🖤

  • @funnymoment9164
    @funnymoment9164 9 месяцев назад

    Thanks!

  • @velocity1186
    @velocity1186 10 дней назад

    Your teaching is superb. You have a new subscriber.

    • @nikoo28
      @nikoo28  8 дней назад

      Thanks a lot 😊

  • @ajaykumar-yk7to
    @ajaykumar-yk7to Год назад +1

    super sir good explanation

  • @bhumikabansal6022
    @bhumikabansal6022 10 месяцев назад

    SO CLASSY AND please make the playlist of data structures important questions too

    • @nikoo28
      @nikoo28  9 месяцев назад

      playlist: ruclips.net/p/PLFdAYMIVJQHM8Kh5i8P2lGIbJXFPBelRI

  • @albingeorgekurian4396
    @albingeorgekurian4396 16 часов назад

    it's tough to get an optimized solution 😔.... but I will try to reach it on my own 😊.

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

    great video

  • @subee128
    @subee128 7 месяцев назад +1

    Thanks

  • @tng3100
    @tng3100 4 месяца назад

    the second soln was impressive,

  • @palanivelraju1981
    @palanivelraju1981 Год назад +3

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

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

    You're the best👍💯

  • @LalitSingh-nd5vx
    @LalitSingh-nd5vx 5 месяцев назад

    better explanation than Striver .

  • @ahmedbaig8543
    @ahmedbaig8543 7 месяцев назад

    Very Well Explained

    • @nikoo28
      @nikoo28  6 месяцев назад +1

      Thank you so much 🙂

  • @arnavkukreti2009
    @arnavkukreti2009 10 месяцев назад

    perfect teacher

    • @nikoo28
      @nikoo28  9 месяцев назад +1

      perfect student :)

  • @yash_14h
    @yash_14h 10 месяцев назад +1

    Why there are only three types of numbers? In array

    • @nikoo28
      @nikoo28  9 месяцев назад

      you can have as many types.

  • @enriquegrageda
    @enriquegrageda 6 месяцев назад

    Thanks man, good explaining, if i land a job, ill send you some money 😁

    • @nikoo28
      @nikoo28  5 месяцев назад

      haha..thanks a bunch

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

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

    *Sir Please Complete top 150 interview Questions First from Leetcode 🔥*

    • @nikoo28
      @nikoo28  Год назад +3

      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!

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

  • @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 Месяц назад +1

      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]