edited by
1,253 views
0 votes
0 votes

An array $'A'$ has $n$ distinct integers. What is the tightest time complexity to check $A[i]=i$ for some $i$. Consider all elements of array within range from $1$ to $n$.

  1. $O(n^2)  $
  2. $O(1)$
  3. $O(n)$
  4. $O(logn)$
edited by

3 Answers

3 votes
3 votes

O(logn) is correct In case of sorted array. 

o(n) in case of unsorted array. 

edited by
2 votes
2 votes
Answer should be O(n) if Array is unsorted as we need to go element by element and check if A[i] == i

if the given array is sorted then we could solve in O(1) because elements are from 1 to n and if array is starting from index 0 then we can directly return

FALSE ( there is no such element in the array where A[i] == i )

if array is starting from index 1 then A[1] == 1 so return TRUE
1 votes
1 votes

The above question will be solved in O(n) time since our input is not sorted.

Apply linear search, So, O(n).

If the input would have been in sorted order we could have just applied Binary Search on index and got the answer in O(log n) time.

I hope it helps :)

Answer:

Related questions

1 votes
1 votes
2 answers
1
akankshadewangan24 asked Sep 20, 2018
884 views
If t(n) and s(n) denotes the time and space complexity of an algorithm with input size n element then which one of the following is always true?S(n)=O(t(n)) correct H...
0 votes
0 votes
0 answers
2
Kai asked Jan 29, 2017
444 views
Time complexity of the given program is?
1 votes
1 votes
1 answer
3
2 votes
2 votes
1 answer
4