CareerCup Given a sorted array which contains scores. Write a program to find occurrence

265 篇文章 1 订阅
86 篇文章 0 订阅
Given a sorted array which contains scores range from 0 to 100. Write a program to find occurrence of given score. 
eg. 1,1,1,40,40,40,100,100 
input: 40 
o/p : 3 (as 40 appear 3 times in array)

---------------------------------------------------------------------------------------------------------------------------------

Solution 1 : I said binary search for score in this example 40 which in turn will give and index and then have two pointer one goes left and one go right and count the 40. 
worst case O(n) if all numbers are 40. 
Solution 2 : Modify the binary search such that when element does not exists it return s low index. and then do binary search 39.5 and 40.5 (score-0.5 and score +0.5) 
searching 39.5 will give you index 2 which is one less than start index of 40. 
searching 0.5 will give you index 6 which is one more than end index of 40. 
He did not ask me to code this just want to have idea. I told him we need to handle some edge condition.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值