Majority Element | GeeksforGeeks

  Рет қаралды 54,170

GeeksforGeeks

GeeksforGeeks

8 жыл бұрын

Explanation for the article: www.geeksforgeeks.org/majority...
This video is contributed by Harshit Jain.

Пікірлер: 53
@jiechaowang5960
@jiechaowang5960 5 жыл бұрын
why can't when we loop through the array, we have a map to keep track of each element and its count, and when the count is greater than n/2 we print out that element?
@killaknut
@killaknut 5 жыл бұрын
You can do that, but space complexity will be higher
@dinakarmaurya8000
@dinakarmaurya8000 6 жыл бұрын
isMajority() method can be optimized.. if(count > size/2) is in loop so calculate the size/2 out side the loop to optimize the algorightm
@mohammadmujahid8850
@mohammadmujahid8850 4 жыл бұрын
When we have find the candidate element, which is a majority element, then why we again need to ceck whether its a majority element or not?
@utkarshjain1797
@utkarshjain1797 4 жыл бұрын
when we find the candidate element, it means we find the element having maximum occurrence in that array. But, to become majority element its count must be greater than n/2. So, that case we have to check.... I think you understand buddy...
@harinmehta1551
@harinmehta1551 3 жыл бұрын
This can also be solved using Bit Manipulation. 0. make a frequency array [32] 1. Traverse thru the array and for each element : 1.1 for(int i = 0; i < 31; i++) { if( (arr[k] & i) != 0 ) freqArray[i]++; } 2. Traverse thru frequency array and for all indicies where frequency > n/2, add 2^(index) to ans
@dhruvakadipu
@dhruvakadipu 3 жыл бұрын
For those who're confused, don't waste your time; here's your answer. Answer: Here "Majority" means "MORE THAN ( n / 2 )". Example: 2 2 3 2 -> Majority = 2 Example: 2 1 2 3 -> Majority = Something Else This algorithm only makes sense when it is GURANTEED that an element appears more than n/2 times. So, don't misunderstand the name of the function like I did.
@axel9194
@axel9194 2 жыл бұрын
Thx bro
@shivamkansagara4929
@shivamkansagara4929 2 жыл бұрын
thanks bro
@reyou7
@reyou7 6 жыл бұрын
why would someone down vote this video? it is nicely explained.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
Haha....thanks for the appreciation. :)
@chandreshchitalia9341
@chandreshchitalia9341 6 жыл бұрын
will this algo work in java
@AnnoyingErrors41
@AnnoyingErrors41 3 жыл бұрын
def majorityNumberMoore(A,N): majority_index, counter = 0,1 for i in range(1,N): if A[majority_index] == A[i]: counter += 1 else: counter -=1 if counter == 0: majority_index = i counter = 1 candidate = A[majority_index] seen = 0 for i in range(N): if A[i] == candidate: seen += 1 if seen > N//2: return candidate return -1 Can someone improve the run time?
@hermesmercuriustrismegistu4841
@hermesmercuriustrismegistu4841 4 жыл бұрын
Thx a lot for the video!
@onlywilddrift9506
@onlywilddrift9506 7 жыл бұрын
moores method doesn't work when majority element is greater than index 0 . example test case-4,6,2,4,5,0,2,2,2,2,2,2,2,2,2
@niteshkumar-cv3ls
@niteshkumar-cv3ls 6 жыл бұрын
its working bro
@naveenojha5603
@naveenojha5603 6 жыл бұрын
how it is o(n)
@wecan2729
@wecan2729 3 жыл бұрын
int majorityElement(int a[], int size) { mapmp; for(int i=0;isecond>k) { return it->first; } } return -1; }
@joysarkar548
@joysarkar548 5 жыл бұрын
sort the array and return the median
@Yatogami1706
@Yatogami1706 5 жыл бұрын
What if there isn't a majority element?
@gauravkungwani
@gauravkungwani 4 жыл бұрын
@@Yatogami1706 before returning check for the median if its a majority element or not
@SuyashSoni248
@SuyashSoni248 6 жыл бұрын
for 2,2,3,8,7,9,9,6 majority elt is 9 but it must be 2. someone plz explain.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
A majority element in an array A[] of size n is an element that appears more than n/2 times . Here, there is no majority element. Neither 9 nor 2. Can you please tell us why do you think otherwise?
@vibekdutta6539
@vibekdutta6539 4 жыл бұрын
If the array were like 2,2,2,1,3,4,5,6,6 in this case I get 6 as the majority element why? When it should be 2
@trglegend6287
@trglegend6287 Жыл бұрын
here majority element is the element which repeat more number of time here 2 repeat more so ans is 2
@funwithrahulbulbul3767
@funwithrahulbulbul3767 5 ай бұрын
​@@trglegend6287their is no majority element since the count of 2 is < n/2
@mahenderk2149
@mahenderk2149 7 жыл бұрын
It wont work for below no's : 2,2,3,8, 7,9,9
@adeete09
@adeete09 7 жыл бұрын
It works.There is no majority element in this array and the moore's voting algo will print none in this case
@mahenderk2149
@mahenderk2149 7 жыл бұрын
yes...working...thnq
@SuyashSoni248
@SuyashSoni248 6 жыл бұрын
for 2,2,3,8,7,9,9,6 majority index is 5 and majority elt is 9 but it must be 2. someone plz explain
@utkarshjain1797
@utkarshjain1797 4 жыл бұрын
#include using namespace std; // Moore's Voting Algorithm int majority(int arr[],int n) { int i,pos,count; pos = 0; count = 1; for(i=1;i>n; int arr[n]; for(i=0;i>arr[i]; } res = majority(arr,n); if(res == -1) { cout
@Paradise-kv7fn
@Paradise-kv7fn 5 жыл бұрын
There are multiple ways to solve this questions and each approach has its trade off's between the space and time complexity. 1) Brute force with TC O(n^2) and SC O(1) 2)Sort the array and then return the middle element(median) with TC O(nlogn) and SC O(1) 3)Use a HashMap with TC O(n) and SC O(n) 4)Voting algorithm with TC O(n) and SC O(1). Now I was able to think of the first 3 approaches but how the hell can you think of the fourth approach and that too in an interview? :/
@devanggupta9986
@devanggupta9986 4 жыл бұрын
In method 3 -> for (1,1,1,1,2,4,5,6,8) the programme is printing none whereas it should print 1 can any one explain why???
@kalagaarun9638
@kalagaarun9638 4 жыл бұрын
I've been wondering the same !!
@gurucharan1117
@gurucharan1117 4 жыл бұрын
i guess cause 1 dint repeat for more than 9/2-- times
@Noone-kl6sc
@Noone-kl6sc 3 жыл бұрын
9/2 is 4.5 So the no. Of occurrence of 1 should be 5 or more.. So ans is none
@arjuns.3752
@arjuns.3752 5 жыл бұрын
First sort the array using in built function . Traverse through the sorted array and check if an element is equal to its next one If it is then count++ Else count=1 As soon as it reaches greater than half of array size,print that element and break the loop and condition=1. Lastly if condition!=1 then print "No majority element" :D Problem Java??
@beastyt2510
@beastyt2510 5 жыл бұрын
I've done exactly this thing on gfg. But it shows TLE!! Btw same way of thinking lol
@programmingstuff4031
@programmingstuff4031 6 жыл бұрын
check this case a[ ]={2,2,3,4,2,2,3,3} here majority element is 2 but I will return 3 as candidate .
@nikhilnagireddy754
@nikhilnagireddy754 6 жыл бұрын
the element should be present more than n/2 times then it is majority element
@spicytuna08
@spicytuna08 6 жыл бұрын
using map stl from C++ simplifies solution. #include #include using namespace std; int main(void) { //int iA[] = { 3, 3, 4, 2, 4, 4, 2, 4, 4 }; //int N = 9; int iA[] = { 3, 3, 4, 2, 4, 4, 2, 4 }; int N = 8; map mapDS; int i; bool bFound = false; float fMajority = N/2; for( i=0; i fMajority ) { cout
@coder_1037
@coder_1037 5 жыл бұрын
but if u will submit in GOG it will be give tle!!!
@rahulvig5298
@rahulvig5298 4 жыл бұрын
@@coder_1037 can you help me understand why will this give a tle? I know what a tle is, just wanna know why using maps in c++ stl would give a tle.
@KAMALKAMAL-ig6qc
@KAMALKAMAL-ig6qc 4 жыл бұрын
{2,2,3,5} ke lie 5 aa raha hai candidate
@your_dad_18
@your_dad_18 4 жыл бұрын
Wrong
@LukeThompson
@LukeThompson 7 жыл бұрын
Algorithm dont work u sped
@sysshenry
@sysshenry 7 жыл бұрын
can you explain more?
Detection of Loop in a Linked List | GeeksforGeeks
10:10
GeeksforGeeks
Рет қаралды 98 М.
Majority element in an array | Bitmasking
11:25
Techdose
Рет қаралды 16 М.
Was ist im Eis versteckt? 🧊 Coole Winter-Gadgets von Amazon
00:37
SMOL German
Рет қаралды 37 МЛН
Alat Seru Penolong untuk Mimpi Indah Bayi!
00:31
Let's GLOW! Indonesian
Рет қаралды 16 МЛН
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 107 МЛН
Dutch National Flag Algorithm. Explained with playing cards.
12:11
Two elements whose sum is closest to zero | GeeksforGeeks
13:18
GeeksforGeeks
Рет қаралды 27 М.
Find the Number Occurring Odd Number of Times | GeeksforGeeks
6:28
GeeksforGeeks
Рет қаралды 46 М.
Count Strictly Increasing Subarrays | GeeksforGeeks
10:00
GeeksforGeeks
Рет қаралды 21 М.
Find the minimum distance between two numbers | GeeksforGeeks
15:56
GeeksforGeeks
Рет қаралды 65 М.
(Remade) Leetcode 169 - Divide And Conquer | Majority Element
5:17
Nideesh Terapalli
Рет қаралды 11 М.
Pythagorean Triplet in an array | GeeksforGeeks
11:04
GeeksforGeeks
Рет қаралды 33 М.
Moore voting algorithm
7:46
Techdose
Рет қаралды 98 М.
Majority element | Divide and Conquer | Leetcode 169
5:12
Deep Coding
Рет қаралды 4,3 М.