An array a [], with N elements, admitting repeated, is said to "contain a v element mostly" if more than half of its content equals v. Given the array a [], it is intended to draw an efficient algorithm (at time N.log (N) and using divide-and-conquer) to check if it contains a majority element and to determine it. Consider the only available comparison operation between elements of the array, is the equality (a [i] == a [j]), performed in constant time. (Hint: In the algorithm, divide the array to [] into two subarrays a1 [] and a2 [], each one half the size of a []. If the element in most of a [] is v, then v must be also the element in majority of a1 [], or a2 [] or both).
int main() {
int a[12] = {5, 9, 3, 13, 5, 21, 5, 7, 17, 12, 5, 6};
int N = 12, lo = 0, hi = N - 1, mid,i,j;
mid = lo + (hi - lo) / 2;
int n1 = mid - lo + 1;
int n2 = hi - mid;
int a1[n1],a2[n2];
/* Copy data to temp arrays a1[] and a2[] */
for (i = 0; i < n1; i++)
a1[i] = a[lo + i];
for (j = 0; j < n2; j++)
a2[j] = a[mid+1+j];
while (i < n1 && j < n2) {
if(a1[i]==a2[j]){
}else if(){
}else{
}
}
return 0;
}
Im having troubles on the way I should proceed using the operation of equality comparing the auxiliar arrays to see if the most element is on a1[] or a2[] or both!