This is the first time I am working on an analysis of a class of algorithms and am wondering if anyone can help in the example below. I believe I solved this for O (n) complexity, but wondered if there is a better version that I don't think about (logn) about?
Let A = A [1] <= ... <= A [n + 1] be a sorted array of n different integers in which each integer is in the range [1 ... n + 1]. That is, exactly one integer from {1, ..., n + 1} is absent in A. Describe the efficeint algorithm to find the missing integer. Analyze the complexity of the worst case (the number of accesses to array A) of your algorithm.
The solution that I have is relatively simple, and I believe that the results are in the worst case complexity N. Maybe I'm already thinking about this example, but is there a better solution?
My decision
for(i = 1; i < n +1; i++) : if(A[i-1] > i) : return i
The logic behind this is that since it is sorted, the first element should be 1, the second should be 2, etc. etc., while the element in the array is larger than the element that it should be, the element indication was omitted, return the element that it should be, and we have the missing one.
Is this the correct logic? Is there a better way to do this?
Thank you for reading and in advance for your help.
sorting arrays algorithm asymptotic-complexity
Busturdust
source share