Majority Element I | Brute-Better-Optimal | Moore's Voting Algorithm | Intuition 🔥|Brute to Optimal

Поделиться
HTML-код
  • Опубликовано: 25 авг 2024

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

  • @takeUforward
    @takeUforward  11 месяцев назад +22

    Please watch our new video on the same topic: ruclips.net/video/vwZj1K0e9U8/видео.html

    • @suchitakulkarni2744
      @suchitakulkarni2744 2 месяца назад

      Bhai have a doubt .. after the loop if we check cnt is not equal to 0 and return el then also the program executes properly then why we are again taking second loop ..?

    • @pallavi22222
      @pallavi22222 2 месяца назад

      @@suchitakulkarni2744 The first loop will only give you the majority element, the second loop is used to check whether the frequency of the element from the first loop is greater than n/2. You can clearly understand this by watching once again the video from 13.00 where raj bhai explains by changing the last 4 elements to 1.

    • @ShreyasinghIITPATNA
      @ShreyasinghIITPATNA Месяц назад

      int majorityElement(vector v) {
      int n=v.size();
      sort(v.begin(),v.end());
      return v[n/2];
      } this is also accepted and time complexity is O(nlogn) space is O(1)

    • @aadeshkumar819
      @aadeshkumar819 17 дней назад

      @@suchitakulkarni2744 if the problem statement says that there will always be a majority element then you don't need the second loop but if it doesn't guarantees a majority element then you have to check. timestamp : 17.00 . hope this clears

  • @factsmadeiteasy9943
    @factsmadeiteasy9943 Год назад +168

    No matters how hard the paid courses gang will try to defame you or stop you , we STUDENTS will never stop promoting you among our friends and support you in your journey.❤❤

  • @aamirarshad4536
    @aamirarshad4536 Год назад +489

    one leetcode a day keeps unemployment away

  • @346_PrachiRaut
    @346_PrachiRaut 4 месяца назад +40

    "sitting in an interview u cannot invent the algorithm" LMAOOOO xd

    • @akashgite905
      @akashgite905 Месяц назад +1

      tell them brute force then 🙂

  • @takeUforward
    @takeUforward  Год назад +116

    Let's march ahead, and create an unmatchable DSA course! ❤
    Use the problem links in the description.

    • @harshavardhan184
      @harshavardhan184 Год назад +7

      Hatsoff to this consistency

    • @CodeBoost8375
      @CodeBoost8375 Год назад +2

      Time Stamp
      0:43 - Problem Statement
      1:45 - Brute Force Solution
      2:58 - Better Solution
      4:46 - Code (Better Solution)
      6:58 - Optimal (Moore's Voting Algorithm)
      7:38 - Dry run + Intuition
      14:37 - Code
      16:52 - Time complexity
      17:36 - Outro

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

      Hey Striver bro , It will be humble request from myside that could you please explain the approaches in Hindi&English language if possible. It will be very useful for many of students like me.

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

      You dont have to ask for likes RAJ. You will get them even if you dont say. THis level of top notch content will not go un-noticed.

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

      Wow hats off ❤❤❤🎉

  • @bhubeshsr6281
    @bhubeshsr6281 Год назад +40

    Striver Bhai Level of Dedication and hardwork u put on this is .....

  • @CodeBoost8375
    @CodeBoost8375 Год назад +20

    Understood! Very well.None of them can match your dedication and energy.🔥🔥🔥🔥
    And when you explain how algorithm is working the first thing comes in my mind damm! Yarr ye to kisi ne bataya hi nahi tha how the cnt is working.

  • @lisachakraborty528
    @lisachakraborty528 7 месяцев назад +9

    u make me fall in love with coding there was a tym i hated it i started late but yes you are true inspiration

    • @user-hp9kj8qt1h
      @user-hp9kj8qt1h Месяц назад +1

      I'm hating it now. Not able to build intuition myself. Always dependent on video solutions. Sometimes I feel I'm too dumb and not built for this thing. What should I do 😭😭

  • @ashishbm3565
    @ashishbm3565 10 месяцев назад +9

    very great intutions bhaiyaa!!! you are a legend. never thought i would be enjoying dsa and leetcode like this... i dont care i get a job or not but i have to finish this just for the thrill!!!

  • @shubhamagarwal1434
    @shubhamagarwal1434 21 день назад +1

    #Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India..

  • @vaibhav1247
    @vaibhav1247 Месяц назад +3

    better : sorting O(nlogn)
    int majorityElement(vector& nums) {
    int n= nums.size();
    sort (nums.begin(),nums.end());
    return (nums[n/2]);
    }
    optimal is the same : moore's

    • @sachinthakkar5027
      @sachinthakkar5027 Месяц назад +1

      I also thought the same but there are two things here,
      1. It should be given that the majority element always exists for given test cases.
      2. Time complexity wise Moore's is still better.

    • @vaibhav1247
      @vaibhav1247 Месяц назад

      @@sachinthakkar5027 yes

  • @ArcGaming07YT
    @ArcGaming07YT Год назад +42

    Let me help u guys understand :
    point 1 : moores law gives ans when there is strictly majority element present in the array.
    point 2 : use this example to understand it in clear way [1,2,1,3,1,4,1,5,1]
    if last last number was not 1 then it clearly means that there is no majority element in array
    else its 100% that it will be the last element in above example
    point 3 : understand it more by seeing these examples
    [1,2,3,4] gives ans as 4 but its not valid array as (there is no majority element in it)
    point 4 : there is no chance that we will get any other number other than majority number as ans,if there is strictly majority number present in the array and if the array is not only the valid array i,e(there is no majority element in it) then ans can be any value.

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

      👏👏

    • @anmolprasad6813
      @anmolprasad6813 Год назад +2

      Thanks bro, I was so confused on this point (We could get 1 as the ans despite it not being majority) but your comment cleared it

    • @AffairWithGeo
      @AffairWithGeo Год назад +4

      So this is valid if there is strictly a majority, else not possible?
      [2 2 2 1 1 1 3 3] as per the video logic 3 comes as majority. But N/2 = 8/2 = 4 to be an answer, answer has to satisfy, answer > 4. So seems like no way to . . . .???
      Plz help to make things clear

    • @aryanpinto5105
      @aryanpinto5105 Год назад +6

      @@AffairWithGeo The question does mention that there does exist a majority element. Your test case itself is invalid becoz it doesn't have any majority element.

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

      i dont know why but you really helped me😊😊

  • @debasmitamondal3519
    @debasmitamondal3519 Месяц назад

    I could write all the three approaches without even looking at the solution. Striver's lectures have made my coding skills made this strong. Literally grateful !!

  • @Rohan-ce1sy
    @Rohan-ce1sy 10 месяцев назад +2

    Very good explanation. Better than any paid DSA course in market. Thank you so much Striver bhai.

  • @aadityabuchale15
    @aadityabuchale15 Год назад +19

    Please Take care of your health Striver bhaiya.... because i am seeing dark circles around your eyes...📢📢

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

    Understood! Bhai never gone through such type of explanation ....thank you very much....

  • @cinime
    @cinime Год назад +5

    Understood! Super fantastic explanation as always, thank you so much for your effort!!

  • @Manishgupta200
    @Manishgupta200 Год назад +8

    Super Duper Amazing... Your explaination from brute-better-optimal is fantastic with Time and Space complexity. Moor's Voting Algorithm explained by you is really awesome without using any extra space.
    The best thing like once in a blue moon type.. I liked the most is you explain in a unique way in every tutorial.. Here is.. like.. How Time complexity is O(N) not added (because in problem, array have atleast one majority element. So, here TC < O(N) always at the time of checking majority) in Moore's Voting algorithm at the time of checking 'el' is majourity or not.. And also you explain Space Complexity also. Thankyou Striver for such an amazing tutorial 🔥.

  • @deepalivarshney23
    @deepalivarshney23 Месяц назад +1

    literally you are amazing .

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

    Loved the way you explained brother. You're a true magic man

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i 2 месяца назад +1

    completed and understood!

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

    Consistency 🔥 🔥.
    Thanks for all you do. I really appreciate

  • @padduchennamsetti6516
    @padduchennamsetti6516 15 дней назад

    understood very well,you are a gem

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

    Understood!
    Moore's Voting Algorithm is Awesome.

  • @shadowslayer2248
    @shadowslayer2248 3 месяца назад

    Understood and practiced the code as always! Let's really Take Ourselves Forward!!

  • @supriyamanna715
    @supriyamanna715 Год назад +2

    Buk e amar gorom rokto, amra striver er vokto!
    great content!

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

      Nice one 😂👌

  • @dixitnakhva6181
    @dixitnakhva6181 6 месяцев назад +2

    2 things:
    1) this algo will give you majority element but it can be true for only last some portion of array(i.e 2 3 2 3 1 algo will give you 1 but it's only true for last portion of array which [1] and it's of size 1 but it's not true for whole array) , now comes to 2nd step
    2) algo give you probable majority element, now check whether it's correct or not by iterating over an array
    In case of 2 3 2 3 1
    Algo gives you 1 as probable majority element
    Now, by applying 2nd you will find out that 1 is not majority element if we consider whole array of size 5
    So, it means array doesn't have any majority element
    Because if it does, it would have caught by algo
    But the probable majority element that algo gives us (1) is also not an answer, so there is no majority element in an array

    • @rakeshnagarkar8759
      @rakeshnagarkar8759 28 дней назад

      I stuck with the algorithm thinking that it only works with last portion of array to find majority element but your excellent explanation removed my confusion, thank you.

    • @dixitnakhva6181
      @dixitnakhva6181 19 дней назад

      @@rakeshnagarkar8759 you're welcome dear

  • @impalash_ag
    @impalash_ag 3 месяца назад

    Hi Raj,
    Thanks for the video.
    As per your explanation, one more slight optimisation we can do in Moore's Voting Algorithm if we're not sure whether majority element exists. Say we have array size n and n%2==0(means and even size array) and if our count value at last is also 0 in that case we do not need to go to the 2nd step i.e. traverse the whole array again to get the occurrence of element(ele variable value). Hope it makes sense.

  • @ParasSharma-mf8or
    @ParasSharma-mf8or Год назад +2

    0:00 Introduction of course
    0:46 Majority Element(>n/2)
    1:48 Brute approach
    2:17 Pseudo code (Brute)
    2:56 Better solution
    4:45 Code-compiler (Better solution)
    6:59 Optimal(Moore's Voting Algorithm)
    7:56 Algorithm explanation+Dry run
    14:37 Code-compiler (Optimal approach)

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

    Understood
    Thank You :-)

  • @manipandit18
    @manipandit18 Год назад +19

    Understood !!!
    Time Stamp
    0:43 - Problem Statement
    1:45 - Brute Force Solution
    2:58 - Better Solution
    4:46 - Code (Better Solution)
    6:58 - Optimal ( Moore's Voting Algorithm )
    7:38 - Dry run + Intuition
    14:37 - Code
    16:52 - Time complexity
    17:36 - Outro

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 5 месяцев назад +1

    Understood Sir.............Awesome lec.......

  • @ritik3514
    @ritik3514 3 месяца назад

    i just watch the video ,understand the concept but doesn"t code right away i try to solve the same question on leetcode after 1-2 days .this relly help me to use my own mind while code . and amazing job striver for making coding simpler

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

    Thank you bro i got it

  • @SinghDigna
    @SinghDigna Месяц назад

    if array is must having a majority element then do this :-
    public int majorityElement(int[] nums) {
    int count = 0 , el = 0;
    for(int i = 0 ; i < nums.length ; i++){
    if(count == 0){
    el = nums[i];
    count=0;
    }
    if(el == nums[i]){
    count++;
    }else{
    count--;
    }
    if(count > nums.length/2){
    break;
    }
    }
    return el;
    }

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

    Thank You Striver Bhaiya for Such a Quality Content...

  • @aurobindsahu4355
    @aurobindsahu4355 Год назад +2

    Understood &&&&&& Love from odisha bhaiya

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

    nicely explained. even though I slept while watching the video, I woke up again and understood the solution xD
    thanks

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

    huge respect for you
    no words 💖

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

    what a WONDERFULL EXPLANATION sir,understood every thing,please keep making these kinds of videos for us sir, please

  • @tholetigaargyreddy2613
    @tholetigaargyreddy2613 Месяц назад +1

    understood🥰🥰🥰🥰🥰best DSA videos everr

  • @vigneshkrishnan3766
    @vigneshkrishnan3766 Год назад +2

    you can use this solution too........ sort(a.begin(),a.end());
    int mid=a.size()/2;
    return a[mid];

  • @anilkumar4200
    @anilkumar4200 Год назад +2

    Thanks for lectures from bottom of my heart.Keep doing good work.

  • @ShikharRaiyat
    @ShikharRaiyat 9 дней назад +1

    I think the code could have been much simpler to understand if we would have wrote like this,just a suggestion :)
    int majorityElement(int arr[],int n) {
    int i=0;
    int cnt=0;
    for (int j = 0; j < n; j++)
    {
    if(arr[j]==arr[i]) cnt++;
    else cnt--;
    }
    if(cnt

  • @JAY-bq4kw
    @JAY-bq4kw Год назад

    class Solution {
    public:
    int majorityElement(vector& nums) {
    sort(nums.begin(),nums.end());
    return nums[nums.size()/2];
    }
    };

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

    Understood bhaiya ! Thanks a lot

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

    Very good explanation. Easy to understand. Thanks for posting this video. Hats off to your dedication.

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

    Oh God dimag hil Gaya pura. How did someone invent so innovative algorithms and how can someone explain it so lucidly. Striver daa you are just CODING GOD. Uff 🔥🔥🔥. May God bless you always. ❤️❤️

  • @MaheshPatil-of1zy
    @MaheshPatil-of1zy 5 месяцев назад +7

    Seating in Interview you cant discover this algorithm🤣🤣🤣🤣🤣

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

    Done .. Concept clear but want to do revise ❤😊 pls like so it remind me to watch again for revision thankyou bhaiyaa love uh ❤

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

    Completed 7/28!!!

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

    Looking forward more such videos from you bhaiya , you are awesome.

  • @berserker556
    @berserker556 3 месяца назад +1

    Very well explained

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

    You dont have to loop through the array in the end to verify the count bro
    count++ and count-- => when this becomes 0 it ensures that total elements so for is 2n and when the final count is > 0 it ensures that the element with count !== 0 will have count > n/2

  • @_CodeLifeChronicles_
    @_CodeLifeChronicles_ 3 месяца назад

    oh my god the best course ever

  • @Nithishkumar-bx9jf
    @Nithishkumar-bx9jf 5 месяцев назад

    Understood! Really enjoyed your DSA lecture series ❤

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

    @takeUForward , awesome explanation, really appreciate your efforts. The way you explain stuff is really remarkable.
    I see a small bug in the code
    class Solution:
    def majorityElement(self, nums: List[int]) -> int:
    ele = None
    count = 0
    for num in nums:
    if count == 0:
    ele = num
    # count += 1

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

    Thanks for your support❤

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

    wow really good and interactive lecture

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

    Thank you for such in-depth explanation and course❣

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

    Understood, Thank for explaining it so beautifully.

  • @watch2-grow
    @watch2-grow 4 дня назад

    Understood thank you bhaiya

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

    understood. I am going to make that interviewer happy.

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

    Understood . Thank you so much bhaiya for such a clear explanation.

  • @niharj5294
    @niharj5294 3 месяца назад

    i thought of an approach in each we sort the given array and return the element present at the index N/2 ..... this approach is taking O(nlogn) time and O(1) space complexity.

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

    Hamara driver top gear me gaadi chala raha hai 🔥🔥🔥😁

  • @user-pc2pn6yo8r
    @user-pc2pn6yo8r 8 месяцев назад

    Understood bro, your explanation is top notch..

  • @torishi82
    @torishi82 Месяц назад

    Amazing work. Respect!!

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

    i will watch the next video now

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

    Just no words to thank you 🙏🙏🙏

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

    great explanation of moore voting algo..

  • @konankikeerthi
    @konankikeerthi 2 месяца назад

    Understood bro. Thank you for your efforts

  • @manan-543
    @manan-543 Год назад

    Understood. Your explanation is amazing.

  • @anuragbhardwaj13
    @anuragbhardwaj13 Год назад +2

    While solving the problem I wrote my own solution, it's O(nlogn) because we need to sort the array but it's passing all testcases and even time complexity test :
    just have a look at it :
    static int majorityElement(int a[], int size)
    {
    // your code here
    //sorting array here will take O(nlogn)
    Arrays.sort(a);
    int count=0;
    //traversing array once O(n)
    for(int i=0;i0&&a[i]>a[i-1]){
    count=0;
    }
    //else increasing count
    count++;
    //checking majority
    if(count>size/2){
    return a[i];
    }
    }
    return -1;
    }

    • @AdityaKumar-be7hx
      @AdityaKumar-be7hx Год назад +4

      If you sort the array then the answer is simply the element at n/2 index it the sorted array. Nothing to do after sorting

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

      @@AdityaKumar-be7hx 😂😂😅

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

      ohhh

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

    int majorityElement(vector v) {
    // Write your code here
    int cnt=1;
    int ans;
    sort(v.begin(),v.end());
    for(int i=0;i v.size()/2) {
    ans=v[i];
    }
    }
    else{
    cnt=1;
    }
    }
    return ans;
    }
    This can also be better approach

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

    Love the way u teach, wonderful... thnks

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

    thanks striver understood everything

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

    sort(nums.begin(),nums.end());
    int n = nums.size();
    return nums[n/2]; we can also use this bcoz the ques suggests majority>n/2 so it will always occupy the n/2 th index.

  • @RamKumar-dm6es
    @RamKumar-dm6es Год назад

    "Sitting in interview , You can't invent this algorithm" funny line.
    And Understood

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

    Thanks for making concept soo simple to understand :)

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

    wonderful way of teaching!!! Understood!

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

    Understood. Thanks a lot . Please make more videos

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

    GREAT man on this earth

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

    @takeUforward, Bhayia this course is really awesome.💯

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

    Understood very well brother.

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

    Beautiful algorithm, need to be on quality crack to come up with this in an interview.

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

    sort the array and simply return a[n/2] element always true

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

    Pta ni comment ap tak pahuchega ki ni bt this means alot t us which i can describe in the words ❤

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

    I really appreciate your hard work, bhaiya. Love from the core of my heart from Bangladesh 💝💝

  • @RajNamdev_19
    @RajNamdev_19 2 месяца назад +1

    Understood.

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

    understood everything sir awesome video

  • @yashagarwal7042
    @yashagarwal7042 3 месяца назад

    Very Great Explanation

  • @Mel-up7un
    @Mel-up7un Месяц назад +1

    understood!

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

    Understood. Awesome explanation

  • @VarunKaushal-zx9zq
    @VarunKaushal-zx9zq Год назад

    Understood, thank you so much

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

    very good feeling confident

  • @shyam.upadhyay
    @shyam.upadhyay 9 месяцев назад

    Awesome content bro, love you!

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

    thankyou striver!!!! we appreciate your work☺☺

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

    fantastic explanation, you really went over the algo well. and its such a brilliant algo too. i initially solved with a hashmap, but this is much much better since constant space,

  • @MandapatiGoutham
    @MandapatiGoutham Месяц назад

    Great lecture😇