edited by
1,516 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

939
views
2 answers
1 votes
akankshadewangan24 asked Sep 20, 2018
939 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 How???
478
views
0 answers
0 votes
Kai asked Jan 29, 2017
478 views
Time complexity of the given program is?
803
views
1 answers
1 votes
1.7k
views
1 answers
2 votes
Tushar Shinde asked Jan 24, 2016
1,660 views
Which of the following statement is true?Binary insertion sorting (insertion sort that uses binary search to find each insertion point) requires $O (n \log n)$ total ... can find the next smallest element to a given element in $O(1)$ time.