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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ส.ค. 2024

ความคิดเห็น • 84

  • @ruthikapamar7981
    @ruthikapamar7981 6 หลายเดือนก่อน +7

    Slick & straight! Thank you for breaking the complexity.

  • @sheldoncooper7990
    @sheldoncooper7990 10 หลายเดือนก่อน +3

    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.

    • @nikoo28
      @nikoo28  10 หลายเดือนก่อน

      thanks for the kind words

  • @michaelagedie9433
    @michaelagedie9433 6 หลายเดือนก่อน +3

    Keep making these videos bro, the quality is Amazing!

  • @Ramu_Sriram
    @Ramu_Sriram 6 หลายเดือนก่อน

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

    • @nikoo28
      @nikoo28  5 หลายเดือนก่อน +1

      I wish that too 😄

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

  • @akhintheruvath
    @akhintheruvath 29 วันที่ผ่านมา

    Thanks for this great explanation

  • @rakeshbabu3839
    @rakeshbabu3839 28 วันที่ผ่านมา

    Amazing exaplanation

  • @Rits2203
    @Rits2203 ปีที่แล้ว +1

    You've explained in a very simple way!!

  • @Hello-l3i
    @Hello-l3i หลายเดือนก่อน

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

  • @negxativexd2622
    @negxativexd2622 7 หลายเดือนก่อน

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

    • @nikoo28
      @nikoo28  7 หลายเดือนก่อน

      Thanks for liking

  • @satyamgupta6030
    @satyamgupta6030 ปีที่แล้ว +1

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

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      So nice of you

  • @ayushijindal4898
    @ayushijindal4898 ปีที่แล้ว +1

    Beautifully Explained

  • @velocity1186
    @velocity1186 หลายเดือนก่อน

    Your teaching is superb. You have a new subscriber.

    • @nikoo28
      @nikoo28  หลายเดือนก่อน

      Thanks a lot 😊

  • @m.abrarsheikh9865
    @m.abrarsheikh9865 2 หลายเดือนก่อน

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

    • @nikoo28
      @nikoo28  หลายเดือนก่อน

      that is so sweet of you

  • @arnavkukreti2009
    @arnavkukreti2009 29 วันที่ผ่านมา

    great explanation

  • @user-gy7hz5tn1w
    @user-gy7hz5tn1w 7 หลายเดือนก่อน +1

    this was very helpful 😀 Thank you

  • @hameedmulani21
    @hameedmulani21 ปีที่แล้ว +1

    Superb Explanation!

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      Thank you 🙂

  • @amanverma8258
    @amanverma8258 ปีที่แล้ว +1

    Thanks a lot sir ! Really helpful

  • @albingeorgekurian4396
    @albingeorgekurian4396 28 วันที่ผ่านมา

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

  • @SandipKumarRoy12
    @SandipKumarRoy12 11 หลายเดือนก่อน +1

    Awesome explanation 😮😮

  • @bhumikabansal6022
    @bhumikabansal6022 11 หลายเดือนก่อน

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

    • @nikoo28
      @nikoo28  10 หลายเดือนก่อน

      playlist: th-cam.com/play/PLFdAYMIVJQHM8Kh5i8P2lGIbJXFPBelRI.html

  • @kunalkheeva
    @kunalkheeva ปีที่แล้ว

    thank you, your dry run really helped

  • @marcelocarvalholopes
    @marcelocarvalholopes 6 หลายเดือนก่อน

    Thank you. Very good explanation!

  • @mehbubrohit12
    @mehbubrohit12 10 หลายเดือนก่อน

    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  9 หลายเดือนก่อน +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 :)

  • @MythBuster28_10
    @MythBuster28_10 5 หลายเดือนก่อน

    Glad i found your channel

  • @LalitSingh-nd5vx
    @LalitSingh-nd5vx 6 หลายเดือนก่อน

    better explanation than Striver .

  • @tng3100
    @tng3100 5 หลายเดือนก่อน

    the second soln was impressive,

  • @ajaykumar-yk7to
    @ajaykumar-yk7to ปีที่แล้ว +1

    super sir good explanation

  • @shubhammanecr7
    @shubhammanecr7 ปีที่แล้ว +1

    Amazing!

  • @TakshaSharma
    @TakshaSharma ปีที่แล้ว +1

    amazing amazing!!!!!

  • @subee128
    @subee128 8 หลายเดือนก่อน +1

    Thanks

  • @CelestialEditzHub
    @CelestialEditzHub ปีที่แล้ว

    Amazing great explanation

  • @yash_14h
    @yash_14h 11 หลายเดือนก่อน +1

    Why there are only three types of numbers? In array

    • @nikoo28
      @nikoo28  10 หลายเดือนก่อน

      you can have as many types.

  • @riddle_cicad007
    @riddle_cicad007 6 หลายเดือนก่อน

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

  • @ahmedbaig8543
    @ahmedbaig8543 7 หลายเดือนก่อน

    Very Well Explained

    • @nikoo28
      @nikoo28  7 หลายเดือนก่อน +1

      Thank you so much 🙂

  • @arnavkukreti2009
    @arnavkukreti2009 11 หลายเดือนก่อน

    perfect teacher

    • @nikoo28
      @nikoo28  10 หลายเดือนก่อน +1

      perfect student :)

  • @CSstudent_1001
    @CSstudent_1001 10 หลายเดือนก่อน

    LEGEND 🖤

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

  • @tarunbhatt5003
    @tarunbhatt5003 ปีที่แล้ว

    great video

  • @funnymoment9164
    @funnymoment9164 10 หลายเดือนก่อน

    Thanks!

  • @hameedmulani21
    @hameedmulani21 ปีที่แล้ว

    You're the best👍💯

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      You are!

  • @enriquegrageda
    @enriquegrageda 6 หลายเดือนก่อน

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

    • @nikoo28
      @nikoo28  6 หลายเดือนก่อน

      haha..thanks a bunch

  • @dineshkinibailoor340
    @dineshkinibailoor340 7 หลายเดือนก่อน

    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  7 หลายเดือนก่อน +1

      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 3 หลายเดือนก่อน

    Can we do using 2 pointer

    • @nikoo28
      @nikoo28  3 หลายเดือนก่อน

      Give me a pseudo code for your approach

  • @mdsalik9374
    @mdsalik9374 9 หลายเดือนก่อน

    What if there is no majority element? How to handle that?

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      then it will be an entirely different problem. What are you looking to find?

  • @foodandjournieswithme8788
    @foodandjournieswithme8788 4 หลายเดือนก่อน

    It gives error when nums=[6,5,5]

    • @nikoo28
      @nikoo28  4 หลายเดือนก่อน

      What error are you getting? I tried the case again and it gives 5 as the output

    • @foodandjournieswithme8788
      @foodandjournieswithme8788 4 หลายเดือนก่อน

      @@nikoo28 sir i got output as 6 in the same code

    • @nikoo28
      @nikoo28  4 หลายเดือนก่อน

      @@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 2 หลายเดือนก่อน +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++)

  • @mdsalik9374
    @mdsalik9374 9 หลายเดือนก่อน

    Can we get n/3 solution as well?

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      do you have a link to the problem?

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