We all know how to search through an array for an element whose value equals the target value, but how to search for the element that has value greater than the target value?

A particularly elegant way of thinking about this problem is to think about doing a binary search over a transformed version of the array, where the array has been modified by applying the function

f(x) = 1 if x > target
0 else

Now, the goal is to find the very first place that this function takes on the value 1. We can do that using a binary search as follows:

int low = 0; high = numElems;
while (low != high) {
int mid = (low + high) / 2; // Or a fancy way to avoid int overflow
if (arr[mid] <= target) {
/* This index, and everything below it, must not be the first element
* greater than what we're looking for because this element is no greater
* than the element.
*/
low = mid + 1.
}
else {
/* This element is at least as large as the element, so anything after it can't
* be the first element that's at least as large.
*/
high = mid;
}
}
/* Now, low and high both point to the element in question. */

Reference: http://stackoverflow.com/questions/6553970/find-the-first-element-in-an-array-that-is-greater-than-the-target

Binary search for the first element greater than target的更多相关文章

  1. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  2. 【leetcode】1038. Binary Search Tree to Greater Sum Tree

    题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  4. my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏

    If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...

  5. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. stl binary search

    stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...

  7. Remove Node in Binary Search Tree 解答

    从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...

  8. 35. leetcode 501. Find Mode in Binary Search Tree

    501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the  ...

  9. LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

随机推荐

  1. HttpClient(4.3.5) - HTTP Request & HTTP Response

    HTTP Request All HTTP requests have a request line consisting a method name, a request URI and an HT ...

  2. Linux 命令 - arp: 操作系统的 ARP 缓存

    arp 命令可以查看 ARP 缓存或者手动添加.删除缓存中的条目. 命令格式 arp [-evn] [-H type] [-i if] -a [hostname] arp [-v] [-i if] - ...

  3. WinForm 实现登录,验证成功,关闭登录界面,显示主界面

    点击登录按钮时: ") { this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show(" ...

  4. Cocos2d-x中触摸事件

    理解一个触摸事件可以从时间和空间两方面考虑. 1.触摸事件的时间方面 触摸事件的在时间方面,如下图所示,可以有不同的“按下”.“移动”和“抬起”等阶段,表示触摸是否刚刚开始.是否正在移动或处于静止状态 ...

  5. 技术博客rss订阅源收集

        http://blog.sina.com.cn/rss/2506410862.xml http://fullrss.net/a/http/www.cocoachina.com/cms/rss. ...

  6. ios 多线程-GCD-NSOperation

    一.线程间的通讯 1.使用NSObject类的方法performSelectorInBackground:withObject:来创建一个线程. 具体的代码:隐式创建,自动启动 [Object per ...

  7. javascript的排序算法

    已经准备秋招一段时间了,因为这个关系也在各种巩固知识,顺便整理一下一些东西.这篇文章就是自己整理了一下各种JS的排序算法,以便自己以后回顾. 冒泡排序 function bubbleSort(arr) ...

  8. L002-oldboy-mysql-dba-lesson02

            L002-oldboy-mysql-dba-lesson02 [root@web01 ~]# mysql -uroot -ptestpassword mysql> use mys ...

  9. 巧用Systemtap注入延迟模拟IO设备抖动

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: 巧用Systemtap注入延迟模拟IO设备抖动 当我们的IO密集型的应用怀疑设备的IO抖动,比如说一段时间的wait时间过长导致性能或 ...

  10. DTCMS一些问题

    站点管理,主站和手机站同时绑定不同域名 手机站会报错,解决方法为 主站不绑定 手机站绑定 关键问题为:不能和主站域名相同 PC模版文件下的JS文件夹下的commen.js和手机模版下的JS文件夹下的b ...