retagged by
287 views
0 votes
0 votes

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)                                                                      (b) O (log n)

(c) O (n)                                                                      (d) None of these

Solution: Option (c)

retagged by

2 Answers

1 votes
1 votes

The question asks us to find any i such that A[i] = i.

So we start from i, checking if A[1] = 1, A[2] = 2 and so on.

Thus, we will have to check till A[n] = n, making it $O(n)$ and not $O(1)$.

0 votes
0 votes
This is a linear search problem where in we check for index value and element at that index.It is not O(1) as it is not constant which is independent of the input size,here the above problem is dependent on the input size hence it cannot be O(1).

O(logn) is possible as we can do binary search to find elements but it is not tightest upper bound hence O(n)

Related questions

0 votes
0 votes
0 answers
1
Akriti sood asked Dec 27, 2016
368 views
in an effort to make MERGE-SORT faster, you decide to divide the array into k equal sized, disjoint subarrays, where k 2. This means that you have to merge k lists. How ...
0 votes
0 votes
3 answers
2
radha gogia asked Jul 17, 2015
987 views
If we talk about that since since we cant access any random element in a linked list for that reason quick sort cant be used for linked lists ,then in merge sort also we ...
1 votes
1 votes
3 answers
3
khushboo123 asked May 25, 2016
1,088 views
int fun(int n){ int count = 0; for (int i = n; i 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; }then this function should give O(nlogn).since outer lo...