Majority Element in an Array - Moore's Voting Algorithm - Amazon Interview Question

  Рет қаралды 21,945

PrepBytes

PrepBytes

Күн бұрын

Пікірлер: 47
@AnandKumar-uy7nn
@AnandKumar-uy7nn 3 жыл бұрын
That's how one should approach any programming problem's solution!! Great Explanation maam !!
@siddhantkumar698
@siddhantkumar698 4 жыл бұрын
Fantastic explanation .. love from IIT KGP
@shantanudhore4880
@shantanudhore4880 4 жыл бұрын
@satyaprakash4598
@satyaprakash4598 4 жыл бұрын
Really commendable .... Low to high ... This is how logic builds.
@prepbytes
@prepbytes 4 жыл бұрын
Thanks!
@sachinsoma5757
@sachinsoma5757 2 жыл бұрын
Great explanation, you made it look simple, subscribed .
@ashutoshranjan4644
@ashutoshranjan4644 2 жыл бұрын
Good explanation mam! Understand it completely
@ayushagarwal4689
@ayushagarwal4689 2 жыл бұрын
mam, in this question after sorting mid element will be the majority element as majority element occurs more than n/2 times .so, we can write it as, in python, nums.sort() return nums[len(nums)//2]
@hareenkumardevu4636
@hareenkumardevu4636 3 жыл бұрын
Excellent explanation mam 💯 .... Thnq mam... Keep making videos
@vivekprasad5669
@vivekprasad5669 4 жыл бұрын
Amazing explanation
@tarunbawari2858
@tarunbawari2858 5 жыл бұрын
It can be solved in o(n) times by another method that we use before watching our video rather than using Moore algorithm Int b[]={0}; for(int i=0; i
@ssquare1
@ssquare1 5 жыл бұрын
The 2nd for loop should go till max element of a[i] As it will fail for this testcase N= 4 a[i]={5,5,5,5}
@KARTIKKUMAR-lj9lz
@KARTIKKUMAR-lj9lz 5 жыл бұрын
here time complexity is very large. eg. let one of the element in the array is 10^1000 , then you need a array of size 10^1000. Better , you can use hashMap but that also worse case space complexity is 0(n)...
@tarunbawari2858
@tarunbawari2858 5 жыл бұрын
@@ssquare1 sorry bro my second loop is wrong i doesn't see at that time but instead of that you can use hashmap it will work for testcase you will give unordered_mapmp; for(int i=0; isecond>c) { cout
@kausachan4167
@kausachan4167 3 жыл бұрын
space and time tradeoff
@nktrendings816
@nktrendings816 3 жыл бұрын
Super Explanation Madam
@lalkrishna1251
@lalkrishna1251 4 жыл бұрын
If it is given that , majority element exists for sure , then element at majority_index will be the majority element. Because only that majority element can survive this process till end.
@writobratabandyopadhyay721
@writobratabandyopadhyay721 5 жыл бұрын
Very nice explanation...i have a request...please can you do a video explaining the time and space complexity...please...please...it would be a great help...😃😃
@prepbytes
@prepbytes 5 жыл бұрын
Thanks, sure we will do it soon
@mohitlalwani3505
@mohitlalwani3505 4 жыл бұрын
is this algo work for N/3 ?
@aakashkatiyar5005
@aakashkatiyar5005 4 жыл бұрын
leetcode se aya h?
@siddhantkumar698
@siddhantkumar698 4 жыл бұрын
Nope
@amitejpratapsingh2702
@amitejpratapsingh2702 4 жыл бұрын
O(n) using hash map. No need to sort the array.
@VishalSharma-hq5ti
@VishalSharma-hq5ti 4 жыл бұрын
what if we aren't allowed to use extra space?
@sasmitshubham9424
@sasmitshubham9424 4 жыл бұрын
take care of space complexity
@A7503-d4i
@A7503-d4i 5 жыл бұрын
Loved it .. Please make videos on zco , zio , INOI & IOI
@alphazero0
@alphazero0 4 жыл бұрын
the awesome video broke down the concepts and how to the answer very well.
@shenth27
@shenth27 2 жыл бұрын
It's hard to come up with this kinda algorithm during the interview. simpler solution I could think of just sort the array, maintainMaxCount and majorityElement, iterate through the array and if (arr[i-1] != arr[i]) then execute a while loop inside as long as arr[i] == arr[i+1] and count the occurance of arr[i]. if anytime the current count is larger than the maxCount, update it and also assign majority element. time complexity will be nlogn + n.
@sarthakverma5921
@sarthakverma5921 5 жыл бұрын
nice explanation. this can also be solved by using hashmaps
@gokulnathnallaiya7617
@gokulnathnallaiya7617 4 жыл бұрын
Moore tc-O(n),sc-O(1) Hash tc 0(n), sc-O(n)
@omar-vr1kg
@omar-vr1kg 4 жыл бұрын
using hash might take more space..if in a interview if he asks for only time complexity then yeah it will work
@AmandeepSingh-ot6st
@AmandeepSingh-ot6st 4 жыл бұрын
One doubt ! .... If we are traversing any array twice or thrice or 4 times .... Will our time complexity still remain O(n) or it is n*O(n) ????
@JB-ve7yh
@JB-ve7yh 4 жыл бұрын
If we traverse the array twice, it will be 2O(n). If we traverse the array 3 times, it will be 3O(n). If we traverse the array n times, it will be n*O(n). HOWEVER, all of these are still considered O(n) runtime and therefore reduces to O(n).
@omar-vr1kg
@omar-vr1kg 4 жыл бұрын
actually it will be just a multiple of a constant. Since O(k*n) is same as O(n) for some small constant like 3,4 in front of n..so yeah time complexity will be O(n) only
@deepakpal3209
@deepakpal3209 4 жыл бұрын
Hi can anyone take this array {2,1,1,4,2,9,2,3,5,3,2,6,3} and apply the same logic that is explained in the video. here after sorting the mid is 3 but here my question is 2 occurrence is more than 3. then how this algo is working fine.
@rehanchoudhury1937
@rehanchoudhury1937 4 жыл бұрын
You dont have any majority element here , The condition is that an element(in ur case 2) to be a majority element it must appear more then n/2 times( 2 appeared only 4 times where your n/2 is 13/2 =6.5 so we consider floor value that is 6 i.e 2 must have appeared atleast 6 times to be majority) No majority element as per question condition
@omar-vr1kg
@omar-vr1kg 4 жыл бұрын
This algo is only for finding majority element...like since majority element will be more than n/2 by count it will endup being as a middle element.. think it like this For every sorted array- Majority element will be middle element in all cases But middle element wont be majority element in all cases
@AjayKumar-ju6sl
@AjayKumar-ju6sl 3 жыл бұрын
I want to know WHY WHY WHY ?? this algorithm works .....
@RajdeepBarman
@RajdeepBarman 3 жыл бұрын
Try the sequence [2,3,7,7,7] starting with 2 as majority and count = 1 when we at 3 at index 1, since it's not equal to the majority (now 2), count decrements. Hence, now count = 0. Therefore, majority is now the current element 3 and count is 1 when we are at 7 at index 2, what happened in the previous step repeats. With 7 now being majority and count = 1 Coming to your question as to why this algorithm works; it's because, if you observe closely, when you are moving through the elements incrementing and decrementing count as per the algorithm the majority element (since they are more in number) will eventually set itself as the majority even if we don't start with it being the majority element. Therefore, when you are at the end it's going to be the majority. It's an extremely smart algorithm. Hats off to Robert S. Boyer and J. Strother Moore. One would have to be extremely ingenious to arrive at this algorithm, let alone in an interview
@sairam6873
@sairam6873 4 жыл бұрын
int main(){ int arr[10]={2,2,2,2,7,7,7,3}; sort(arr,arr+10); int c=1; for(int i=0;i=4){ cout
@abhishekshankar1136
@abhishekshankar1136 4 жыл бұрын
bro this is going to take O( nlogn ) because youre sorting , to do it in O(n) you need moore voting algo
@ShivamJha00
@ShivamJha00 2 жыл бұрын
Bruh if you're sorting it anyway, then arr[n/2] is always going to be your majority element (assuming a majority element always exists)
@uttamkarmakarece3534
@uttamkarmakarece3534 3 жыл бұрын
Ma'am if you can slow down a bit while teaching it would be great for every students I guess... otherwise great explaination
@savantdude
@savantdude 4 жыл бұрын
the explanation is garbage!
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Counting inversions in an array
19:03
Techdose
Рет қаралды 94 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
Boyer Moore Majority Vote Algorithm
5:52
AlgosWithMichael
Рет қаралды 21 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 196 М.
Majority Element II | Brute-Better-Optimal
26:58
take U forward
Рет қаралды 220 М.
Find Majority Element in an Array using Binary Search Tree O(n logn)
16:51
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 3,5 М.
Two Pointer Algorithm | Two Sum Problem | Solve DS Problems in O(N) Time
19:18
JAVAAID - Coding Interview Preparation
Рет қаралды 123 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН