Lab 4
Lab 4
#include <stdio.h>
count = 0;
for (int i = 0; i < n; i++) {
if (A[i] == majority_in_B) {
count++;
}
}
if (count > n / 2) {
return majority_in_B;
} else {
return -1;
}
}
}
int main() {
int A[] = {3, 3, 4, 4, 4, 4, 2, 4};
int n = sizeof(A) / sizeof(A[0]);
return 0;
}
Time Complexity:
Best Case Scenario:
Majority element at the last position A[n].
As it is at the last position , occurrence of last element is counted first and
it exceeds n/2 and no further recursion is needed.
Therefore, time complexity is O(n).