No video

Majority Element I | Majority Element II : Boyer-Moore | Made Simple | Leetcode - 229 and 169

  Рет қаралды 8,055

codestorywithMIK

codestorywithMIK

Күн бұрын

iPad PDF Notes - github.com/MAZ...
This is the 63rd Video on our Arrays (1-D & 2-D) Playlist.
In this video we will try to solve a very very famous array Problem - Majority Element I and Majority Element II (Leetcode-229 & Leetcode-169).
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Majority Element I and Majority Element II
Company Tags : AMAZON | GOOGLE
My solutions on Github (C++ & JAVA) :
Majority Element - I github.com/MAZ...
Majority Element - II github.com/MAZ...
Leetcode Link :
Majority Element - I leetcode.com/p...
Majority Element - II leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Approaches Summary :
Majority Element I - The algorithm iterates through the array, maintaining a count variable and a candidate variable. It initializes the count to 0 and the candidate to NULL. For each element in the array, it checks if the count is zero. If so, it sets the count to 1 and updates the candidate to the current element. If the current element is equal to the candidate, it increments the count; otherwise, it decrements the count. The algorithm does not explicitly check the frequency of the candidate, as it is guaranteed that a majority element exists. Finally, it returns the majority element found using this approach.
Majority Element II - It employs a variation of the Boyer-Moore Voting Algorithm to identify two potential majority elements (maj1 and maj2) and then validates their frequencies in a second pass. The algorithm initializes two candidates (maj1 and maj2) and their corresponding counts (count1 and count2) to NULL and 0, respectively. It also calculates the required frequency threshold (freq) based on the array size. The first loop iterates through the array, updating the counts and candidates based on the Boyer-Moore Voting Algorithm. In the second pass, the algorithm counts the occurrences of maj1 and maj2 in the array and checks if they meet the frequency criteria. The elements that satisfy the condition are added to the result vector, which is then returned. This approach ensures that the elements included in the result vector appear more than ⌊ n/3 ⌋ times in the given array.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook

Пікірлер: 110
@shivamtomar9658
@shivamtomar9658 11 ай бұрын
bhaiya aapko etni depth m knowledge h hr ek topic. bhot hardworking ho bhaiya aap. 🙏
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hi there, I am just like you all. With more practice and consistency we all can gain more knowledge. Thank you for joining this journey with me . Let’s keep it going 💪😇🙏❤️
@shivamtomar9658
@shivamtomar9658 11 ай бұрын
@@codestorywithMIK thank you so much bhaiya forever ♥
@nagmakhan672
@nagmakhan672 11 ай бұрын
@@codestorywithMIK Hats off to your humbleness. You are so down to earth..
@user-ub2is4rs4x
@user-ub2is4rs4x 6 ай бұрын
@@codestorywithMIKwe are with you in this journey ❤
@aws_handles
@aws_handles 6 ай бұрын
@@codestorywithMIK👌🏻👌🏻
@TheLearningMonk
@TheLearningMonk 11 ай бұрын
Initialize majority1 = NULL and majority2 also, fails the test cases when the array having less then 3 element, so Initialize with INT_MAX. Btw thank you so much bhaiya for those quality videos and easy explanation❤🙏
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@abhayroadlines528
@abhayroadlines528 7 ай бұрын
after watching 1 or 2 videos for the same problem, I stuck with this video and got my mind crystal clear with all of the doubts about the algorithm. Thank you so much❤
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
Thank you ❤️🙏
@Akashkumar_12
@Akashkumar_12 6 ай бұрын
Your explanation is very clear brother and thank you so much 😊
@Ankita-sd5gp
@Ankita-sd5gp 4 ай бұрын
The way you explain even the important caches and makes it easier with story is the most amazing thing. Kudos to you. Really appreciate all your work !
@HarshitKumar-gn5wf
@HarshitKumar-gn5wf 11 ай бұрын
i was solved it with the brute force without using any hint and was very happy because it was my first medium level challenge that i solved on my own, but then after seeing this that there is a more optimal approach , i was happy and sad at the same time :)))
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Never be sad. Trust me this is the process how we learn and grow. Together we are in this journey and we will definitely improve with time. Let’s keep moving 😇🙏
@AlishaKhan-ww3io
@AlishaKhan-ww3io 11 ай бұрын
Wow, i was hoping to find Part I in Your playlist and here i got both 😍 Thanks a lot
@avishkarrangari2328
@avishkarrangari2328 11 ай бұрын
i get to learn something new everyday, thanks for your explanation mate hope to see more in the future .😁
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@newglobal7271
@newglobal7271 11 ай бұрын
Wow , What a great Explaination !!!! maja agya❤❤❤❤❤❤❤❤❤❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@anuppatankar4294
@anuppatankar4294 11 ай бұрын
Great Video 😍
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@deepjyotidas37
@deepjyotidas37 11 ай бұрын
No doubt striver atcha padhata hai ! Maine uske moore voting algorithm dekha tha but samajh mein nahi aya tumhara 2-3 minute dekh-kar hi samaj mein aagaya. Brilliant !!
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
It means a lot to me 🙏❤️ Thank you for watching and trusting my content.
@herculean6748
@herculean6748 11 ай бұрын
wow, I really liked that part when you explained why we need to check the elements first and not blindly update when the count becomes zero
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Glad it helped. Thank you 😇🙏
@herculean6748
@herculean6748 11 ай бұрын
@@codestorywithMIK I watch your videos even after solving the question, because I know I will learn something new :)
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Means a lot 😇🙏
@xiaoshen194
@xiaoshen194 6 ай бұрын
I did majority element 1 in O(1) time complexity 🎉
@MdAli-jc3fe
@MdAli-jc3fe 3 ай бұрын
how ??
@EB-ot8uu
@EB-ot8uu 6 ай бұрын
big fan of your teaching style
@ugcwithaddi
@ugcwithaddi 11 ай бұрын
Best explanation found since morning 👌🏻👌🏻
@Momentsofmagic28
@Momentsofmagic28 11 ай бұрын
Both I and II 🤩 Was waiting for you only sir.
@nagmakhan672
@nagmakhan672 11 ай бұрын
Crystal clear as always. One stop solution 👌
@shashanksharma2258
@shashanksharma2258 11 ай бұрын
Thank you so much bhaiya bahut badhiya samjhaya
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@sachinmohanty4577
@sachinmohanty4577 11 ай бұрын
The algorithm is pretty awesome.. MIK you are just awesome just like this algo ❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
And you guys are awesome too. Thank you so much 😇🙏❤️
@souravjoshi2293
@souravjoshi2293 11 ай бұрын
You are the best bro. Hats off to your knowledge 🔥
@nibaronkumar2484
@nibaronkumar2484 11 ай бұрын
Brother you are doing great job, love from Bangladesh❤❤💕💕
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you so much 😇🙏 Love from India 🇮🇳 ❤️
@nagmakhan3165
@nagmakhan3165 11 ай бұрын
Waited for your videos only 🔥🔥👌
@disastermaster4226
@disastermaster4226 2 ай бұрын
ty sir
@NikhilSharma-qc1ez
@NikhilSharma-qc1ez 6 ай бұрын
Bhai maja aa gya Nice Explaination
@EnglsihCommByNawaz
@EnglsihCommByNawaz 11 ай бұрын
There is a condition diff in Majority Element 2 that is you didn't use else if( nums[i] == majorityElemen1 ) count1++; and else if( nums[i]== majorityElemen2 ) count2++;
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hi there, I did use these two conditions : if(nums[i] == maj1) count1++; else if(nums[i] == maj2) count2++; Can you please elaborate what you mean above ? Thank you 😇🙏
@EnglsihCommByNawaz
@EnglsihCommByNawaz 11 ай бұрын
Sorry Sir ! In Majority Element 1 ques you used this condition that i.e after count condition that i e if(majorityElem == nums[I]) count1++ by fault I'm thinking in Majority 2 question you used the condition above count I'm thinking you forget to mention these conditions after updating the count. But Thanks for explaining in such depth!!
@tutuimam3381
@tutuimam3381 11 ай бұрын
Thanks a lot ❤❤
@JoyAcharyatvl
@JoyAcharyatvl 11 ай бұрын
nice bhai , like always
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@yikes3807
@yikes3807 11 ай бұрын
thank u was waiting for video. love your videos sir best...
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
So nice of you 😇🙏
@EnglsihCommByNawaz
@EnglsihCommByNawaz 11 ай бұрын
One of the best explanation ❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you so much 😇🙏
@sushanthgupta626
@sushanthgupta626 4 ай бұрын
Hi sir i have one simple doubt why we are reducing cnt1 and cnt2 both, in maj 1 question you explained that as some other candidate is increasing instead we can say one candidates vote is decreasing.. similarly here we should have decrease for one candidate only why both?
@sauravchandra10
@sauravchandra10 11 ай бұрын
Clear and concise, thanks
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Glad it was helpful! 😇🙏❤️
@adarshrajpathak0
@adarshrajpathak0 6 ай бұрын
great teaching skill 👏
@mewaconite
@mewaconite 11 ай бұрын
awesome... very very clear solution video .... thank you MIK !🙂
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@aswithasai4015
@aswithasai4015 11 ай бұрын
was waiting for your video thank youu
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hope you enjoyed it! Thank you 😇🙏
@vivekbhardwaj9289
@vivekbhardwaj9289 4 ай бұрын
Sir, Would you please provide the java code for the same. I'm having issue with the wrong answer. (Leetcode 229)
@ShubhamNancheOfficial
@ShubhamNancheOfficial 12 күн бұрын
I used your algo in Java, don't know why it failed at int[] nums = { 1, 1, 2, 3, 4, 1, 1, 5, 6, 7, 1, 1, 8, 9, 10, 1, 11, 12, 13, 14 }; as the test input My code (in Java): public List majorityElement(int[] nums) { // Boyer & Moore's voting algorithm if (nums.length 13 maj2 > 14 // verify if they are actually majority List res = new ArrayList(); int target = nums.length / 3; count1 = 0; count2 = 0; for (int i = 0; i < nums.length; i++) { if (majority1 == nums[i]) { count1++; } else if (majority2 == nums[i]) { count2++; } } if (count1 > target) { res.add(majority1); } if (count2 > target) { res.add(majority2); } return res; }
@mohithadiyal6083
@mohithadiyal6083 11 ай бұрын
The best explanation 😊
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you for watching 😇🙏
@badbaboye
@badbaboye 7 ай бұрын
Thanks
@ashutoshpandey1639
@ashutoshpandey1639 11 ай бұрын
Bhaiya initially first element ko maj1 and maj2 ku nhi bna diya, isme kya dikkat aaegi
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Good Qn and I should have covered this too. Let me try to explain here : What if you assign maj1 = nums[0]; maj2 = nums[1] in the beginning. Let's take an example : [1,1,1,3,3,2,2,2] If you do so, you will be assigning maj1 = 1 and maj2 = 1 as well. This will assume both maj1 and maj2 are same which will be wrong and won't work as per our logic written inside for loop. Really hope this helps. Would suggest you to a complete dry run of this example above. Thank you ♥
@Memerluffy
@Memerluffy 3 ай бұрын
16:37
@newglobal7271
@newglobal7271 11 ай бұрын
One more think sir , please discuss time & space complexity , for this solution will it be(Tc->O(n)+O(n)=O(n) and as it takes only 2 element so the Sc be O(1)?
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
You are absolutely correct 🙌😇
@aws_handles
@aws_handles 8 ай бұрын
hats off
@gauravbanerjee2898
@gauravbanerjee2898 6 ай бұрын
Thanks a lot bhaiya ❤❤
@adarshamit7860
@adarshamit7860 6 ай бұрын
bhaiya leetcode number 3020 ki video hai? which was asked in weekly contest 382, I was not able to find it in your channel ... if somebody found it please share with me!!
@umeshbisht1054
@umeshbisht1054 11 ай бұрын
Thanku bhaiya ❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@code_runner3843
@code_runner3843 11 ай бұрын
Great bhaiya❤
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@dayashankarlakhotia4943
@dayashankarlakhotia4943 11 ай бұрын
class Solution { public List majorityElement(int nums){ Listans=newArrayList >>(); int m1=-1,m2=-1,cnt1=0,cnt2=0; for(int num;nums) if(num==m1){ ++cnt1; }else if(num==m2){ ++cnt2; }else if (cnt1 ==0){ m1=num; ++cnt1; }else if(cnt2 ==0){ m2=num; ++cnt2 ; }else { --cnt1; --cnt2; } cnt1=0; cnt2 =0; for(num:nums) if(num==m1) ++cnt1; elseif (num==m2) ++cnt2; if(cnt1 >nums.length/3) ans.add(m1); if(cnt2 >nums.length/3) ans.add (m2); return ans; } };
@aniketpandey1913
@aniketpandey1913 11 ай бұрын
why are we doing both count1-- and count2-- together and not just count1-- or count2--, I'm not able to understand, have gone through some post but didn't get the intuition
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hi there, Actually if you want to do only count1 - - Or want to do only count2 - - , Then the qn is on what basis we take that decision. So the intuition behind this lies in the core of this Algorithm that “when we see a new element which is neither equal to maj1 or maj2, it is actually a threat (candidate for becoming maj1 or maj2 in future) and hence it will impact both the count1 and count2” Hope this helps 😇🙏
@aniketpandey1913
@aniketpandey1913 11 ай бұрын
@@codestorywithMIK understood thanks
@keertilata20
@keertilata20 11 ай бұрын
BESTTT💙💙
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@Samtoosoon
@Samtoosoon 2 ай бұрын
for(int &num:nums) isme mujhe confusion hoti hai stl walle loop mai
@Samtoosoon
@Samtoosoon 2 ай бұрын
Range based loops got it
@harsh.jain22
@harsh.jain22 11 ай бұрын
bhaiya mai brute force se toh kar leta hu par optimal approach ata hi nhi h mind me , jaise is question me mujhe pta hi nhi tha ye boyer-moore algo ke baare me , toh kya aapne jab ye question pehli baar attempt kiya tha toh : kya aapko is algo ke baare me pehle se pta tha ? ya aapko bhi solutions dekhne padte hai ?
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hi Harsh, The first time when I had seen this qn long ago, I remember I didn’t know about this algorithm. Then I studied and explored this algorithm. Then now I am able to remember this algo and solve this qn. Trust me , this is how we learn and grow with time by seeing more qns.
@naveen_soni_1
@naveen_soni_1 11 ай бұрын
Very nice explained please provide this ppt
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Sure thing. You can find the Link for the PDF notes in the Description. Let me share it here also : github.com/MAZHARMIK/Interview_DS_Algo/blob/master/iPad%20PDF%20Notes/Leetcode-229%20%26%20169%20-%20Majority%20Element%20I%20%26%20II.pdf Thank you for watching ❣
@anikpal7862
@anikpal7862 11 ай бұрын
My initial approach class Solution { public: vector majorityElement(vector& nums) { int n = nums.size(); vector ans; unordered_map mp; for(int i = 0; i < n; i++) { mp[nums[i]]++; } for(auto x: mp) { if (x.second > n/3) { ans.push_back(x.first); } } return ans; } };
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Keep it up 👍🏻
@mathematics7746
@mathematics7746 11 ай бұрын
2223334 isme to highest frequency 2 or 3 ka hai fir dry run krne se to 4 ayega last me candidate ye case kaise handle hoga using this algorithms can you please tell me??
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Here is how the flow will go for your test case : i = 0 maj1 = 2, count1 = 1 i = 1 maj1 = 2, count1 = 2 i = 2 maj1 = 2, count1 = 3 i = 3, maj2 = 3, count2 = 1 i = 4 maj2 = 3, count1 = 2 i = 5 maj2 = 3, count1 = 3 i = 6 count1- - count2- - i goes out of bounds. maj1 = 2 maj2 = 3 So this will work for this test case too. Hope this helps. Thank you for watching 😇🙏❤️
@codeandtalk6
@codeandtalk6 11 ай бұрын
❤❤❤
@sidhanttiwari942
@sidhanttiwari942 11 ай бұрын
majority 2 waale main dono ka count kyu decrease kr rhe thhe?
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Hi there, Actually if you want to do only count1 - - Or want to do only count2 - - , Then the qn is on what basis we take that decision. So the intuition behind this lies in the core of this Algorithm that “when we see a new element which is neither equal to maj1 or maj2, it is actually a threat (candidate for becoming maj1 or maj2 in future) and hence it will impact both the count1 and count2” Hope this helps 😇🙏
@sidhanttiwari942
@sidhanttiwari942 11 ай бұрын
@@codestorywithMIK thanks It makes sense
@dayashankarlakhotia4943
@dayashankarlakhotia4943 11 ай бұрын
my journey comleted one year please refers Low level design playlist
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Well noted 👍🏻
@molyoxide8358
@molyoxide8358 6 ай бұрын
Bro with your permission I will post the video's link in my LC solution. So, pls provide your LC profile link for creidts.
@codestorywithMIK
@codestorywithMIK 6 ай бұрын
Sure 🙏❤️ you can mention my channel name/link
@molyoxide8358
@molyoxide8358 6 ай бұрын
@@codestorywithMIK OK sure bhaiya
@HealthyOm
@HealthyOm 11 ай бұрын
always something specials
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@yadav.nitin03
@yadav.nitin03 11 ай бұрын
Yo🎉
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you 😇🙏
@Samtoosoon
@Samtoosoon 2 ай бұрын
Null kyu kiya maj ko asign samjh nhi aaya
@jelouche
@jelouche Ай бұрын
because maj currently is not know, you can also take it INT_MIN
@Memerluffy
@Memerluffy 3 ай бұрын
13:08
Majority Element II | Brute-Better-Optimal
26:58
take U forward
Рет қаралды 167 М.
SPONGEBOB POWER-UPS IN BRAWL STARS!!!
08:35
Brawl Stars
Рет қаралды 24 МЛН
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 5 МЛН
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 40 МЛН
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 100 М.
Google Coding Interview With A Facebook Software Engineer
49:59
Clément Mihailescu
Рет қаралды 934 М.
Google Coding Interview With A Competitive Programmer
54:17
Clément Mihailescu
Рет қаралды 2,5 МЛН
Winning LeetCode Weekly Contest 198
40:27
William Lin
Рет қаралды 112 М.
SPONGEBOB POWER-UPS IN BRAWL STARS!!!
08:35
Brawl Stars
Рет қаралды 24 МЛН