Unknown_712's blog

By Unknown_712, history, 3 years ago, In English

Given an array A of N elements. Find the majority element in the array. A majority element in an array A of size N is an element that appears more than N/2 times in the array.

Input: N = 3 A[] = {1,2,3} Output: -1 Explanation: Since, each element in {1,2,3} appears only once so there is no majority element.

IF ANY MAJORITY FOUND IN THE ARRAY PRINT IT OTHERWISE PRINT -1.

CAN ANY SOLVE THIS QUESTION IN - TIME COMPLEXICTY O(N) SPACE COMPLEXICITY O(1)

  • Vote: I like it
  • -32
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You have to count the number of each element in array (with variable cnt) then if the maximum count of the elements is less than N / 2 then the answer is -1 else print the maximum element of the array which appears more than other elements !

»
3 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

The majority element appears more than n/2 times in the array. So, if we make two subgroups, one subgroup containing the majority element and the other containing all the other elements then the size of first subgroup will always be larger than size of second subgroup. Now, we keep taking one element from first subgroup and one from second subgroup until the second subgroup is empty. What will be left will be the majority element. So, if you encounter two different elements then either one of them can be a majority. If we remove these two elements then too the majority element among rest should not change. We start with the first index as the candidate for the majority element. If next element is same as candidate the increment the count else decrement the count and make the next element as the candidate for the majority. This is bit hard to grasp. Let's see on an example.

Let the array be [1, 2, 3, 3, 2, 3, 3] At index 0, the candidate is index 0 and count is 1. At index 1, since element is not same as candidate, we decrement count and make it 0 and also make the next element as the candidate element. Keep doing this till the end. 1) If same element as majority, increment count. 2) If different element then decrement count. And if count becomes zero, make next element as the majority.

#include <iostream>
#include <vector>
using namespace std;

int majorityElement(vector<int>& a) {
    int candidate = 0, count = 1, index = 1;
    while (index < (int)a.size()) {
        if (count == 0) {
            candidate = index;
            count = 1;
        }
        else if (a[index] == a[candidate]) {
            count++;
        }
        else {
            count--;
        }
        index++;
    }

    return a[candidate];
}

int main() {
    vector<int> arr = {1, 2, 3, 3, 2, 3, 3};
    cout << majorityElement(arr) << endl;
    return 0;
}

This code assumes that there is majority element present in the array. You can add a check in the end to make sure that count of majority element is indeed greater than n over 2. One test case where we need to check if candidate is majority or not is [1, 2, 3, 4, 5, 6, 7]

So, Time Complexity = O(n + n) = O(2n) = O(n) Space complexity = O(1)

»
3 years ago, # |
  Vote: I like it +13 Vote: I do not like it

let me guess

int majel(const vector<int> &a){
	
	for(int n=size(a), it=100; --it;){
		int i = rnd()%n;
		if(count(ALL(a),a[i])*2>n) return a[i];
	}
	
	return -1;
}
  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Really good technique. Simple and awesome. I've used something similar here. I think it is the only way to get a 100 is to use probabilistic methods not sure though.

»
3 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Boyer-Moore Majority Vote Algorithm

Did you even try to google this? The first link that shows up after searching "Majority element in array" contains the solution.