Majority Element | Leetcode | C++ | Java | Brute-Better-Optimal | Moore's Voting Algorithm

  Рет қаралды 176,045

take U forward

take U forward

Күн бұрын

Пікірлер: 430
@takeUforward
@takeUforward 4 жыл бұрын
Please watch the new video which covers it in more depth, and also prints it: kzbin.info/www/bejne/pIHCn6ZpqribbpI We have updated a new video on the same topic, which covers it in more depth, and c++, java, and python codes as well. Find the video here: kzbin.info/www/bejne/pIHCn6ZpqribbpI
@AnkitKumar-vd2dd
@AnkitKumar-vd2dd 4 жыл бұрын
yes
@arpanbhowmick847
@arpanbhowmick847 4 жыл бұрын
Yes bhaiya
@amitgusain9919
@amitgusain9919 4 жыл бұрын
Take it positive or negative way it's up to u vikram, I have seen u don't stop or ignore if someone says something to you, u are in early days of ur carrier one thing u should keep in mind if u wanna go long way, u should be gentle to ur viewers either they are ur hater or likes u, man these thing will keeping coming ur way the only thing which will keep going is ur behavior aur respect to ur senior or junior or anyone, I know ur hard working, knowledgeable and helping but ur nature or can say telling something which shows some negativity to ur viewers or friends will let u down, hope u understand keep doing good work, all the best.
@takeUforward
@takeUforward 4 жыл бұрын
@@amitgusain9919 if someone comes and hits a dislike to a video right after uploading, I don't think I should be anyways responsible for them. I am responsible for the my takeUforward fam, not for haters gang ;)
@codeWithRisingGeek
@codeWithRisingGeek 4 жыл бұрын
@@takeUforward Understood the intuition behind the algorithm. Regarding the dislikes, I think if there was a scope of improvement, they would have watched the whole video and then hit that dislike button. This was not the case as you told. Anyways, great work!
@nimishgupta8357
@nimishgupta8357 4 жыл бұрын
The intuition was the key in this. Have read Moore algo many times but never had this much clearity 🍻
@mayurkalsekar524
@mayurkalsekar524 2 жыл бұрын
I have lost the hope to come up with such intuitions on my own
@renon3359
@renon3359 4 жыл бұрын
Don't let anybody discourage you brother. You are doing an awesome job. I am going to like this video from all 4 accounts I have. Cheers!
@ssv6055
@ssv6055 2 жыл бұрын
i am going to ruin those 69 likes buwahahaha
@rohanbose4882
@rohanbose4882 4 жыл бұрын
Dislikes are from the scalar academy guys (because their turnover is made by selling these solutions ) :P
@Onchaingambit
@Onchaingambit 4 жыл бұрын
Thanks man for giving out such quality content for free at expense of your valuable time 😊
@dhanashreegodase4445
@dhanashreegodase4445 3 жыл бұрын
You are teaching us such a valuable content for free of cost..thanks alotttttttttttttttttttttt
@xD-qv6vs
@xD-qv6vs 4 жыл бұрын
It's rare people tell about intuition behind their solution... bhaiya you are just so amazing!!! :)
@AdityaKumar-rs5jn
@AdityaKumar-rs5jn 4 жыл бұрын
Yes understood..Finally got to know the intuition behind it.. Great explanation ..
@harshitsrivastava3683
@harshitsrivastava3683 4 жыл бұрын
Yes, I always use brute force approach to solve the problem, it works and i move ahead. Now I know, I am doing wrong. Thanks sir
@adityasaxena3903
@adityasaxena3903 4 жыл бұрын
Absolute Love. Amazed by this algo! Thanks, Striver for this.
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@suguruchhaya3194
@suguruchhaya3194 3 жыл бұрын
(To those who have skepticisim towards the Moore's algo) Striver probably said this somewhere but this is how I tell myself that the algorithm works and I will reiterate: The last segment ("5555" in striver's video) MUSTTTTT be contributing to the fact that a majority element exists. The reason is simple: without the last segment, there will not be a majority element which appears more than half of the time.The given array can only have a majority element IF AND ONLY IF the last segment tilts the balance and actually creates a majority element.
@vibhor3049
@vibhor3049 4 жыл бұрын
Guys, Moore's algo is based on the assumption that you have a majority element(i.e., count > floor(n/2)) in your list. So choose a input where there's definitely a majority element, otherwise you'd think this algo is not working.
@KuldeepSingh-rw6ji
@KuldeepSingh-rw6ji Жыл бұрын
I tried for input [2,2,3,3,3,4,4] and it is giving 4 instead of 3
@kshitizagarwal8389
@kshitizagarwal8389 Жыл бұрын
Size is 7 According to the problem, majority element should occur more than thrice i.e. atleast 4 times. In your case, majority element (3) appeared only thrice.
@ecea044gauravgogoi2
@ecea044gauravgogoi2 3 жыл бұрын
your videos are short and precious , voice is awesome, how can somebody dislike it
@emilyghosh8679
@emilyghosh8679 Жыл бұрын
Excellent explanation ❤loved it
@aasthajha1051
@aasthajha1051 3 жыл бұрын
raj bhaiya hai toh concept clear hone se koi nahi rok sakta!!!!!! thank you so much for this crystal clear explanation!!
@sahukarinaveenkumar3188
@sahukarinaveenkumar3188 4 жыл бұрын
Bro don't think about such people, you are really helping alot for us..
@ROHITSINGH-td9ms
@ROHITSINGH-td9ms 4 жыл бұрын
Arrey bro!! Infact it was your set of codes on gfg(height f tree, etc.) , we look at when it comes to the interviews. Plz don't stop here. Great work 👍
@sachinsoma5757
@sachinsoma5757 2 жыл бұрын
Please ignore hate comments, your videos are really helping and we are really learning from you .
@NatureLover-oq6uc
@NatureLover-oq6uc 2 жыл бұрын
MAN YOU HAVE ENTIRELY CHANGED THE CODING ATMOSPHERE IN THE COLLEGES....NOW EVERYONE IS ACCESSIBLE TO A GOOD CONTENT.
@chiragmehta2955
@chiragmehta2955 2 жыл бұрын
best explaination so far ........especially that text at 9:41 proves that mathematically. thank you
@walkalone1998
@walkalone1998 2 жыл бұрын
Numbers occupying more than half the space must be in the middle when sorted class Solution { public: int majorityElement(vector& nums) { sort(nums.begin(), nums.end()); return nums[nums.size()/2]; } };
@aasmohammad2651
@aasmohammad2651 2 жыл бұрын
no one can stop you bcs you are doing a great job without any greed,
@somyapratapsingh9849
@somyapratapsingh9849 4 жыл бұрын
Your videos make me thing ds and algo are very easy. Hats off to u 👏👏
@debjitmaji632
@debjitmaji632 2 жыл бұрын
If someone still doesn't understand do refer GFG... Very nice explanation
@rishigupta4938
@rishigupta4938 2 жыл бұрын
best dsa sheet and solutions till now,highly appreciable 🙌
@bhaveshkumar6441
@bhaveshkumar6441 2 жыл бұрын
You're the best teacher on this planet. Thank you for explaining this so clearly
@shubhamsingh-hp3dk
@shubhamsingh-hp3dk 11 ай бұрын
this helped me a lot in an assignment
@shiershdwivedi9268
@shiershdwivedi9268 3 жыл бұрын
Yes, You are taking us forward!
@mdbahauddin1252
@mdbahauddin1252 4 жыл бұрын
thanks 'striver' for helping us , please ignore them who dislike your videos. They are nothing but always spoil the life of other because they never taste the success of any kind . We are with you ! keep it up! :)
@aniketshukla8085
@aniketshukla8085 Жыл бұрын
The C++ code shown by striver(09:53 Mins), assumes that if majority element is not present in prefix then it is going to be the last value of "candidate". For example: If we put input array as: 7 7 5 7 5 1 5 7 5 5 7 7 10 10 10 10 Answer is going to be : 10. This would be true for variant of same question which says that, there always exist a majority element and till last element if we are not able to find the majority element then it would be last element.
@aniketshukla8085
@aniketshukla8085 Жыл бұрын
The other video which has been made by striver on same question(link is pinned in first comment) returns - 1 if there does not exist any majority element at all.
@rryann088
@rryann088 2 жыл бұрын
this simplifies a lot int majorityElement(vector& nums) { sort(nums.begin() , nums.end()); return nums[ (int)nums.size()/2 ]; } Thanks
@takeUforward
@takeUforward 2 жыл бұрын
And this guarantees that you will be rejected in an interview if you say this even after knowing the solution in video!
@rryann088
@rryann088 2 жыл бұрын
@@takeUforward Is it because of the simplicity of this code or because of not implementing the moores algorithm? Thank you !! I just want to know why I will be rejected in this particular context
@NehaKumari-cx6un
@NehaKumari-cx6un 4 жыл бұрын
Whoever dislike ur videos r not student.. They are ur competitors . So keep making videos👍👍
@stith_pragya
@stith_pragya 2 жыл бұрын
Thank You So Much Striver Brother...................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@the3idiots787
@the3idiots787 3 жыл бұрын
such a great explanation .First year student like me are able to understand it clearly.
@vijaykumar-sl7rp
@vijaykumar-sl7rp 3 жыл бұрын
Those words during the first one minute! 🔥🔥🔥
@yashrajput7386
@yashrajput7386 3 жыл бұрын
If it is not specified that there exist majority element then, There is a catch with this solution it will return the majority element in the last suffix example if arr[]={1,1,2,2,3,3}, returned candidate will be 3 but this is not the majority element Therefore it will become half of the correct solution, finally after the end of for loop in this solution once again count the number of occurrences of candidate element in array and check if it is >floor(n/2) to confirm its majority
@vikasmahajan9602
@vikasmahajan9602 3 жыл бұрын
bro it is mentioned that there will always be a majority element present
@madhubabu3938
@madhubabu3938 4 жыл бұрын
Another approach:: Sort the array and return the middle element (because majority element occurs more than n/2 times in array that means after sorting middle element must be the majority element) Time - O(nlogn) Not best but better than bruteforce
@takeUforward
@takeUforward 4 жыл бұрын
Yes, true that !
@madhubabu3938
@madhubabu3938 4 жыл бұрын
yeah,Kuldeep Singh. But Striver already discussed that approach in the video.
@jatinranpariya1510
@jatinranpariya1510 2 жыл бұрын
TC can be O(n). Remember we only need to find middle element So we don't have to sort whole array. This can be done using nth_element() library function in O(n). Basically nth_element() is an STL algorithm which rearranges the list in such a way such that the element at the nth position is the one which should be at that position if we sort the list. class Solution { public: int majorityElement(vector &nums) { nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end()); return nums[nums.size() / 2]; } };
@Sweety-rx8zq
@Sweety-rx8zq 2 жыл бұрын
Woww great🙌 actually must not care about those who dislikes such great content
@manaswitadatta
@manaswitadatta 4 жыл бұрын
I feel in love with this algo when I saw it the first time. 🖤
@tonystarc9567
@tonystarc9567 4 жыл бұрын
Brother you are a saviour... Thank you for every thing... Just one more thing brother if you could continue bringing atleast 1 video per day then it would be a lot of help... Thank you...
@leapingquark
@leapingquark 3 жыл бұрын
The way you let go of all the negativity at the beginning of the video was amazing. You are going a very good job bhaiya, I have learned a lot from your videos. Don't ever let anyone discourage you!!
@HarshKumar-nh6be
@HarshKumar-nh6be 4 жыл бұрын
There's no one who can not understand from you.. You are super genious😎😎
@praffulbisht8520
@praffulbisht8520 Жыл бұрын
if your one test case is not running i.e. {3,3,4} Then after: if(count==0){ ele = nums[i]; //add count ++; also count++; }
@AbhishekKumar-eh6zy
@AbhishekKumar-eh6zy 3 жыл бұрын
sir your exaplanations are just awesome
@learn9475
@learn9475 2 жыл бұрын
salute explains it all about this man
@anilsuyal
@anilsuyal 3 жыл бұрын
No Dislike can stop my brother! you're amazing brother, huge respect:)
@sagarchippa327
@sagarchippa327 3 жыл бұрын
Great Explanation👌of intuition behind Moore's Voting Algorithm.
@Vishalraj_1
@Vishalraj_1 3 жыл бұрын
I don't understand what they will get by disliking the video or demotivating others. Anyway, You are doing great job. Keep take us forward !!!!
@Nikhil-qd9up
@Nikhil-qd9up 3 жыл бұрын
I think using unordered_map will have T.C as O(n) ???
@AdityaRaj-ix5rg
@AdityaRaj-ix5rg 3 жыл бұрын
The intuition you provided was awesome. Thanks for the video!!
@sanskritisrivastava2242
@sanskritisrivastava2242 2 жыл бұрын
Best explanation! I never understood this concept so clearly until now.. Thankyou so much!!
@gaunikasrivastava7851
@gaunikasrivastava7851 3 жыл бұрын
The person who would have come up with this algorithm might have been an absolute genius 🤯🤯
@rohitraina5059
@rohitraina5059 3 жыл бұрын
I guess it was moore 👀
@toshimudgal7288
@toshimudgal7288 3 жыл бұрын
@@rohitraina5059 lol
@abhisheksanwal1106
@abhisheksanwal1106 2 жыл бұрын
No it is based on pigenhole principle. Idea has been taken from that.
@ayushverma6131
@ayushverma6131 2 жыл бұрын
thank u soo much sir, I think this is the best explanation till now for moore voting algo. :)
@youngshahrukhkhan8179
@youngshahrukhkhan8179 3 жыл бұрын
Very Nice Explanation......Keep making videos
@prakashsingh1107
@prakashsingh1107 2 жыл бұрын
I love watching your content its awesome
@darshantank554
@darshantank554 2 жыл бұрын
Good solution Bit masking is also a good solution but takes O(NlogN base2) time!
@zafarkhan6242
@zafarkhan6242 Жыл бұрын
Awesome sir well explained this topic
@bhaveshkumar6842
@bhaveshkumar6842 3 жыл бұрын
Thank you so much for what you are doing!!! ❤️❤️❤️
@krishgarg5665
@krishgarg5665 4 жыл бұрын
Before hashmap solution we can tell sorting solution. SOrt the array and check if nums[i]==nums[i+n/2]
@anonymousvoid6356
@anonymousvoid6356 2 жыл бұрын
Striver bhaiyya!! Nice explanation!!!!
@ishitakeshawani2997
@ishitakeshawani2997 4 жыл бұрын
You are doing great work. Thank you 😇
@VinayPandey-l9j
@VinayPandey-l9j 9 ай бұрын
Great explanation. Thanks
@atibhu
@atibhu 4 жыл бұрын
Beautifully explained!
@wasimakram5371
@wasimakram5371 4 жыл бұрын
very detailed explanations ! Keep it up bro !
@rhl_
@rhl_ 2 жыл бұрын
Best explaination. Thank you so much.
@takeUforward
@takeUforward 2 жыл бұрын
Glad it was helpful!
@avikumar9046
@avikumar9046 4 жыл бұрын
Striver we all love you......Thanks for all your efforts for us sir..
@yashrastogi3726
@yashrastogi3726 4 жыл бұрын
Great explanation bro. Love this series. thanks
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
what an amazing video thanks a ton. Keep making such awesome videos.
@SuryaPrakash-us6vn
@SuryaPrakash-us6vn 2 жыл бұрын
in hashmaping technique if we change the last three digits to some element x in above example then ........x will be shown as majority element
@prachipadhi703
@prachipadhi703 2 жыл бұрын
Bhaiya , I owe u a lot , tq for the placement series...
@riashrivastava5358
@riashrivastava5358 3 жыл бұрын
bhaiya ,do not focus on hate .you don't even know how many lives you are changing .we will always be grateful
@lovleshbhatt7797
@lovleshbhatt7797 4 жыл бұрын
Reveal the name who gives dislike , we all try to ask them what is wrong with them. Btw awesome series bhai
@archihalder7462
@archihalder7462 2 жыл бұрын
Thank you so much for this great explanation
@joeyaintwaffling
@joeyaintwaffling 3 жыл бұрын
There can be an edge case where the given array has a majority element but the count of the majority element is not more than n/2 like for this example : 3, 3, 4, 2, 4, 4, 2, 4 ; here n is 8 but the majority element according to moore's algorithm will give you 4 but 4 doesn't occur more than n/2 (4 occurs for exactly n/2) times hence the code will fail there. To overcome this write another loop from 0 to n and count the occurrence of candidate element which you got from moore's algorithm if the count is more than n/2 return candidate element otherwise return -1;
@takeUforward
@takeUforward 3 жыл бұрын
The problem stated it will always have. Read the statement..
@joeyaintwaffling
@joeyaintwaffling 3 жыл бұрын
@@takeUforward Oh sorry. Gfg has the same question and I was having this problem with your code that's why tried to help.
@rohalkurup1350
@rohalkurup1350 2 жыл бұрын
@@joeyaintwaffling Thanks , was stuck in this one when the freq is not greater than n/2 , So we just need to iterate again and check the occurrence of candidate element again Great
@devanshmesson2777
@devanshmesson2777 3 жыл бұрын
Thank you so much striver for making this series. It's very difficult to balance corporate life and youtube. Hats off to you brother!
@sandeepnallala48
@sandeepnallala48 3 жыл бұрын
bro never stop these videos : ) striver always Saviour to us : )...
@RajSalla111
@RajSalla111 3 жыл бұрын
You don't even need to mention the dislikes. that button is just invisible to thousands of learners. Thanks for taking us forward.
@atharvakulkarni7141
@atharvakulkarni7141 4 жыл бұрын
That was explained very well!!!
@zeelthumar
@zeelthumar Жыл бұрын
keep it my friend we are with you...
@rohittakalkar9252
@rohittakalkar9252 4 жыл бұрын
Correction to the solution ( test case provided ) I think at the end, there is a need to scan once again to verify whether the majority element found using the algorithm is actually appearing more than n/2 times or not. Take an ex : 7,7,1,7,2,3 | 4,7 | 9,9,7,7 | 5,5,5,5 See here. The possible majority element found using the algorithm is 5. 5 may be the majority element, but it's not. So there is a need to scan once again to verify whether the possible majority element is actually a majority element or not Please let me know if I am wrong. I may be because I am not as experience as you 😇
@takeUforward
@takeUforward 4 жыл бұрын
the problem states it will always have a majority element...
@Plushvoyages
@Plushvoyages 4 жыл бұрын
there is no majority element in this case,as no element is occuring more thann n/2 times.
@prakharagarwal4933
@prakharagarwal4933 4 жыл бұрын
Finally I was able to get the intuition I was been struggling ....
@AbhishekGupta-xz1gd
@AbhishekGupta-xz1gd 2 жыл бұрын
Work for example [1,1,4,3]. Here the majority is 1, but through this algorithm 3 will come as the output
@abhisheksanwal1106
@abhisheksanwal1106 2 жыл бұрын
No majority element exists in your example.
@shouryasingh2193
@shouryasingh2193 4 жыл бұрын
Pyara Explanation bhai 🤘👍
@prasannapm3220
@prasannapm3220 2 жыл бұрын
thank you .Keep the good work going
@sleepypanda7172
@sleepypanda7172 2 жыл бұрын
more power to you. Loving your work
@rryann088
@rryann088 2 жыл бұрын
beautifully explained
@ChethanaNP-jh2tr
@ChethanaNP-jh2tr 7 ай бұрын
Great explanation!!
@vishnuramj4660
@vishnuramj4660 4 жыл бұрын
Dislikes don't stop u making videos keep on going. There are also guys who download your videos watch 10 times and didn't liked yet. If u consider this as a case i do like share and comment😁🥰. Waiting a next videos and all the series to complete. Your loving follower👽
@arpanbhowmick847
@arpanbhowmick847 4 жыл бұрын
Thank you bhaiya Great explanation ❤️
@naman_goyal
@naman_goyal 4 жыл бұрын
Bro Ignore Unlike. you are making the best stuff for our placements .. tysm
@awesomeps10
@awesomeps10 4 жыл бұрын
how the heck is one supposed to come up with this algorithm in the real interview if you haven't read it beforehand....
@anubhavgupta8164
@anubhavgupta8164 3 жыл бұрын
exactly
@lovelymangal501
@lovelymangal501 3 жыл бұрын
That's why we do leetcode
@sandeepnallala48
@sandeepnallala48 3 жыл бұрын
impossible bro , even interviewers know that we go doing leetcode probs atleast
@sjgghosh7677
@sjgghosh7677 3 жыл бұрын
if we could come up with this kind of algorithm in such a short time span....this algorithm would be named after us XD
@nidhish1407
@nidhish1407 3 жыл бұрын
@@sjgghosh7677 Exactly 😂😂
@vishnuramj4660
@vishnuramj4660 4 жыл бұрын
Pls...Continue what u are doing now. One day really it will give some reward 💪
@priyanshumahato5270
@priyanshumahato5270 3 жыл бұрын
dude you are doing such an amazing work always stay motivated
@tannukumari5144
@tannukumari5144 4 жыл бұрын
great work bhaiya, thank you
@mukeshjadhav1064
@mukeshjadhav1064 4 жыл бұрын
Great hard work bhai and motivator too😍😍 really appreciated🔥🔥🔥
@karandeeplamba8004
@karandeeplamba8004 4 жыл бұрын
Sound quality is awesome
@amittiwari360
@amittiwari360 2 жыл бұрын
In 2nd approach (using hash map ) why time complexity in worst case is O(n log n) and not O(n^2). because hashmap in worst case take O(n) and we are using one for loop so it should be O(n*n). Please help
@ratankumar1399
@ratankumar1399 3 жыл бұрын
why this map based approach take TC 0(nlogn)?
@aakashprajapati4836
@aakashprajapati4836 3 жыл бұрын
It was Nice video Explain very clearly ❤️❤️ Keep it up sir 👌🏻👌🏻
Majority Element II | GOOGLE Interview Question | Brute-Better-Optimal
12:45
Хаги Ваги говорит разными голосами
0:22
Фани Хани
Рет қаралды 2,2 МЛН
UFC 287 : Перейра VS Адесанья 2
6:02
Setanta Sports UFC
Рет қаралды 486 М.
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Trapping Rainwater | Brute | Better | Optimal | with INTUITION
23:23
take U forward
Рет қаралды 282 М.
Majority Element II | Brute-Better-Optimal
26:58
take U forward
Рет қаралды 220 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 147 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 362 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 238 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 196 М.
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 506 М.
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 117 М.
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 2,6 МЛН