Majority Element | GeeksforGeeks

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

GeeksforGeeks

GeeksforGeeks

Күн бұрын

Пікірлер: 53
@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 3 жыл бұрын
Thx bro
@shivamkansagara4929
@shivamkansagara4929 2 жыл бұрын
thanks bro
@reyou7
@reyou7 7 жыл бұрын
why would someone down vote this video? it is nicely explained.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
Haha....thanks for the appreciation. :)
@harinmehta1551
@harinmehta1551 4 жыл бұрын
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
@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? :/
@dinakarmaurya8000
@dinakarmaurya8000 7 жыл бұрын
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
@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
@mohammadmujahid8850
@mohammadmujahid8850 5 жыл бұрын
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 5 жыл бұрын
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...
@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?
@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
@chandreshchitalia9341
@chandreshchitalia9341 7 жыл бұрын
will this algo work in java
@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
@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 4 жыл бұрын
9/2 is 4.5 So the no. Of occurrence of 1 should be 5 or more.. So ans is none
@SuyashSoni248
@SuyashSoni248 7 жыл бұрын
for 2,2,3,8,7,9,9,6 majority elt is 9 but it must be 2. someone plz explain.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
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 5 жыл бұрын
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 2 жыл бұрын
here majority element is the element which repeat more number of time here 2 repeat more so ans is 2
@funwithrahulbulbul3767
@funwithrahulbulbul3767 9 ай бұрын
​@@trglegend6287their is no majority element since the count of 2 is < n/2
@joysarkar548
@joysarkar548 5 жыл бұрын
sort the array and return the median
@shubhamd1706
@shubhamd1706 5 жыл бұрын
What if there isn't a majority element?
@gauravkungwani
@gauravkungwani 4 жыл бұрын
@@shubhamd1706 before returning check for the median if its a majority element or not
@hermesmercuriustrismegistu4841
@hermesmercuriustrismegistu4841 4 жыл бұрын
Thx a lot for the video!
@wecan2729
@wecan2729 3 жыл бұрын
int majorityElement(int a[], int size) { mapmp; for(int i=0;isecond>k) { return it->first; } } return -1; }
@naveenojha5603
@naveenojha5603 6 жыл бұрын
how it is o(n)
@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 7 жыл бұрын
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 5 жыл бұрын
#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
@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
@KAMALKAMAL-ig6qc
@KAMALKAMAL-ig6qc 4 жыл бұрын
{2,2,3,5} ke lie 5 aa raha hai candidate
@your_dad_18
@your_dad_18 4 жыл бұрын
Wrong
@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.
@LukeThompson
@LukeThompson 7 жыл бұрын
Algorithm dont work u sped
@sysshenry
@sysshenry 7 жыл бұрын
can you explain more?
Boundary Traversal of binary tree | GeeksforGeeks
5:38
GeeksforGeeks
Рет қаралды 26 М.
Find the missing number | GeeksforGeeks
7:47
GeeksforGeeks
Рет қаралды 129 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 7 МЛН
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 24 МЛН
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 2,2 МЛН
Majority element in an array | Bitmasking
11:25
Techdose
Рет қаралды 16 М.
Convert array into Zig-Zag fashion | GeeksforGeeks
8:53
GeeksforGeeks
Рет қаралды 43 М.
Floor and Ceiling in a sorted array | GeeksforGeeks
10:37
GeeksforGeeks
Рет қаралды 21 М.
Majority Element II | Brute-Better-Optimal
26:58
take U forward
Рет қаралды 194 М.
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 109 М.
Dutch National Flag Algorithm. Explained with playing cards.
12:11
ADS1: Boyer-Moore basics
8:50
Ben Langmead
Рет қаралды 259 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 7 МЛН