retagged by
1,710 views
1 votes
1 votes

. Assume an array A[1….n] has n-elements, and every element of an array is less than or equal to n. An element is said to be majority element, if it occurred in more than n/2 positions of an array. What is the best time complexity to check whether the majority of elements exist or not?

(a) O (log n)                                                                (b) O (n)

(c) O (n log n)                                                             (d) O (n2)

retagged by

1 Answer

6 votes
6 votes
It's a kind of application of count sort. Every element of the array is less than or equal to n (i.e. the elements lie in some given range).

We just need first part of the count sort which counts the occurrences of each element.

      A[ ] is the input array, count[ ] keeps track of the number of times each element of A[] occurs

          for i=1 to A.length

          {  count[A[i]]= count[A[i]] + 1  }

   This loop takes O(n) time.

After this we need one more O(n) scan of count[ ] to check if any entry is more than n/2.

Total time complexity= O(n) + O(n)= O(n).  So, answer is (b).

Related questions

0 votes
0 votes
2 answers
1
Mak Indus asked Nov 10, 2018
385 views
0 votes
0 votes
1 answer
2
Isha Karn asked Dec 5, 2014
370 views
0 votes
0 votes
2 answers
3
Mak Indus asked Nov 10, 2018
285 views
Given an array of distinct integers A[1, 2,…n]. Find the tightest upper bound to check the existence of any index i for which A[i]= i.(a) O (1) ...