您的位置:首页 > 其它

Given an infinite size array with only 0s and 1s and sorted. find the transition point where 0s end

2015-01-18 23:27 603 查看
转自 :http://www.careercup.com/question?id=9691840


Amazon Interview Question Software Engineer / Developers

0of 0 votes27AnswersGiven an infinite size array with only 0s and 1s and sorted. find the transition point where 0s end or 1s start (written test question. coding)
- Avinash Ega on July
04, 2011 Report Duplicate | FlagAmazon Software
Engineer / Developer Arrays AlgorithmEmail me when people comment.More Questions from This Interview4of 4voteThis can be done by modified binary search.....1.At first check the 2^i the index i=0,1,2,....2.If '1' occurs at some point of index [example say 32 i.e. 2^5]3.Apply binary search between 2^4 and 2^5 [in infinite array the problem is we ill not have the info of high to apply binary search....]correct me if i am wrong ,thanks in advance..
- n20084753 on
July 21, 2011 | FlagReply0of 0 votesyours is right I think
- Anonymous on July 26,
2011 | Flag0of 0voteThe idea is to find upper bound for 1 by exponentially increasing the index in power of 2 and then finding the transition index by exponentially decreasing the index.Similar to binary search.Complexity 2*log(N+1) where N is the transition index.

A is the input array
1.  start=0, end=1, mid=0
2.  if((A[start]==0)&&(A[end]==1)) return start;
3.	while(A[end] != 1)
start=end;
end=end<<;1;
4.	while(A[start+1] != 1)
mid=(start+end)/2;
if(A[mid]==0)
start=mid;
else
end=mid;
5. return start;


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐