retagged by
754 views
1 votes
1 votes
Suppose A  is a sorted array and some of the elements are duplicates.  What is the best upper bound to find out the number of elements that are equal to a given key 'k'

a) O(log n)

b) O(n)

c) O(nlogn)

d) O(n2)
retagged by

1 Answer

0 votes
0 votes
The answer should be O(n). In the worst case even if we go by linear search then to find the number of times the duplicate key is present then also it would take O(n) as the array would be traversed once.

if we use Binary search then after the particular key element that we are searching for is found we have to go both in the left and the right direction to count the no. of duplicate elements. So it will require additional time in comparison to O(logn).

Related questions

2 votes
2 votes
1 answer
2
Hirak asked May 21, 2019
1,244 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.Ans should be O(log n) right by do...
5 votes
5 votes
1 answer
3