shachar0n
shachar0n

Reputation: 175

Algorithms | majority element

Im trying to write an algorithm to return true if a majority element exists in an array and false otherwise. edit: i can only tell if two elements are equal. meaning i can't use < or >, only =. edit: the solution should be in a divide-and-conquer method. its runtime shouldnt go over nlogn, and i wrote something in java but im not sure if it is correct and how to calculate the runtime.. here is what i got:

public static boolean MajorityBoolean(int[] arr) {
    int c;
    int n = arr.length;
    for (int i = 0; i < n; i = i + 2) {
        System.out.println("*");
        if ((arr[i] == arr[(i + 1)%n]) || ((arr[i] == arr[(i - 1+n)%n]))) {
            c = 0;
            for (int j = 0; j < n; j = j + 1)
                if (arr[j] == arr[i])
                    c = c + 1;
            if (c > n / 2)
                return true;
        }
    }
    return false;
}

Upvotes: 2

Views: 2052

Answers (3)

coder101
coder101

Reputation: 850

A majority element in an array A[] of size n is an element that appears more than n/2 times

public static boolean find(int[] arr, int size) { 
int count = 0, i, mElement;
for (i = 0; i < size; i++) {
    if (count == 0)
        mElement = arr[i];
    if (arr[i] == mElement) 
        count++;
    else
        count--;
}
count = 0;
for (i = 0; i < size; i++)
    if (arr[i] == mElement)
        count++;
if (count > size/2)
    return true;
return false;
}

Upvotes: 1

amit
amit

Reputation: 178461

The runtime of the described algorithm is O(n^2). The outer loop is executed n/2 times, thus the inner counter j is resetted n/2 times, and thus the inner loop is executed total of O(n^2) times.

I am not sure I am following the logic behind your idea, but here are two approaches how I'd implement it [in high level pseudo-code]:

(1) create a histogram out of the data:

  • create a Map<Integer,Integer> [the key is the element and the value is the number of occurances]
  • iterate the array, and for each element count how many times it appears
  • iterate the histogram and find if there is a unique maxima.
  • If there is - return true, else return false.

This approach is average of O(n) if you use a HashMap as the map.

(2) sort and find max occurances:

  • Sort the array - as the result, all equal elements are adjacent to each other. You can use Arrays.sort(array) for sorting.
  • Count how many times each element appears [similar to the histogram idea], and find if there is a unique maxima. You don't actually need to maintain the data for all elements, it's enough to maintain for the top 2, and at the end to see if they are identical.

This solution is O(nlogn) average + worst case [actually, depending on the sort - merge sort gives you O(nlogn) worst case, while quick-sort gives you O(n^2) worst case, and both are O(nlogn) on average case].

EDIT:

I misunderstood the problem at hand, I thought you are looking if there is a unique maxima. These 2 solutions still fits for the problem, you just need to modify the last step of each [to check if the most occuring element appears more then half of the times, which is again fairly easy and doable in O(n)].

Also, there is another solution: Use selection algorithm to find the median, and after finding it, check if it is a majority element, and return if it is. Since selection algorithm is divide-and-conquer based solution, I think it fits your needs.

Upvotes: 5

MoveFast
MoveFast

Reputation: 3025

Following is an O(n) solution Find Majority Element

Upvotes: 0

Related Questions