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

本文探讨了在一个仅包含0和1且已排序的无限数组中找到从0到1转折点的有效方法。通过修改二分搜索算法,先以指数级别增加索引来找到1的上界,再在该范围内进行二分搜索确定确切位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 :http://www.careercup.com/question?id=9691840


Amazon Interview Question Software Engineer / Developers




4
of  4vote

This 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 FlagReply
0
of  0 votes

yours is right I think

- Anonymous on July 26, 2011 Flag
0
of  0vote

The 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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值