Using the method of double your index until you pass it, then binary search the region you just jumped over (what it looks like your pseudocode is trying to do), the time spent should be O(log 2 n) where n is the index of the item you are searching for.
Using the method of double your index until you pass it, then binary search the region you just jumped over (what it looks like your pseudocode is trying to do), the time spent should be O(log2 n) where n is the index of the item you are searching for. It will take you (log2 n) tries to find the correct region, and then ((log2 n) - 1) tries to find x within that region (since you already searched from 0..n/2, you only need to search n/2..n). Therefore, O(log2 n + log2 n - 1) = O(log2 n).
However, if the "infinite array" does not contain x or any value greater than x, you will never know, because you will search forever.
I mean the worst index x can have is x itself. Hence I used x. – Brahadeesh Mar 17 at 3:25 @Brahadeesh What about duplicates?
1, 1, 1, 1, 1, 2, ...? – Jonathan Mar 17 at 13:20 Oh. I am sorry I forgot to mention. After some time, I was busy in writing the pseudo code, the guy asked me to assume no duplicates.
– Brahadeesh Mar 17 at 14:34 1 Well, then you can simply binary search the region 0, x, since you know that x (the index) cannot be smaller than x (the value you are searching for). Still O(log2 n) (although now n = x). – Jonathan Mar 17 at 14:50.
The sorted array gives it away indeed: binary sort. Worst case scenario: O(lg(n)). There's no faster, assured way of finding it.
Of course, you could just tell him that finding an element in an infinite array will take forever.
And n is undefined for this problem.So, I guess, you have to express the running time in x. – Brahadeesh Mar 16 at 19:17 exactly – Jim Mischel Mar 16 at 23:06.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.