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

preview_player
Показать описание
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) :
Leetcode Link :

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
Рекомендации по теме
Комментарии
Автор

bhaiya aapko etni depth m knowledge h hr ek topic.
bhot hardworking ho bhaiya aap.
🙏

shivamtomar
Автор

Your explanation is very clear brother and thank you so much 😊

Akashkumar_
Автор

Wow, i was hoping to find Part I in Your playlist and here i got both 😍
Thanks a lot

AlishaKhan-wwio
Автор

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 !

Ankita-sdgp
Автор

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❤🙏

TheLearningMonk
Автор

The idea behind the algorithm is that if we keep removing groups of three different elements, any element that appears more than one-third of the time will still be left over.

vivekkumar-rfrk
Автор

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

HarshitKumar-gnwf
Автор

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❤

abhayroadlines
Автор

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

deepjyotidas
Автор

Best explanation found since morning 👌🏻👌🏻

hypewaali
Автор

i get to learn something new everyday,
thanks for your explanation mate hope to see more in the future .😁

avishkarrangari
Автор

Amazing! Thank you so much! You do such a great job of breaking down these problems!

nealsutaria
Автор

Both I and II 🤩
Was waiting for you only sir.

FanIQQuiz
Автор

Crystal clear as always. One stop solution 👌

nagmakhan
Автор

You are the best bro. Hats off to your knowledge 🔥

souravjoshi
Автор

The algorithm is pretty awesome.. MIK you are just awesome just like this algo ❤

sachinmohanty
Автор

Wow , What a great Explaination !!!! maja agya❤❤❤❤❤❤❤❤❤❤

newglobal
Автор

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

herculean
Автор

Brother you are doing great job, love from Bangladesh❤❤💕💕

nibaronkumar
Автор

class Solution {
public List <Integer >majorityElement(int nums){
List<Integer >ans=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;
}
};

dayashankarlakhotia
welcome to shbcf.ru