Binary search for the first element greater than target
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的更多相关文章
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
- 【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 ...
- [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 ...
- 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 ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- stl binary search
stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...
- 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 ...
- 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 ...
随机推荐
- Quartz Scheduler(2.2.1) - Working with TriggerListeners and JobListeners
TriggerListeners and JobListeners Listeners are objects that you create to perform actions based on ...
- vs如何新建自己工程的环境变量(局部)和 Windows系统(全局).
来源:http://blog.csdn.net/jtop0/article/details/7574139 在vs2008的Project->Property设置里经常会看到类似$ ...
- Xcode修改项目名称教程
http://wenku.baidu.com/view/4e939b1cf61fb7360a4c653b
- 操作Excel导入的问题(转)
当Excel导入成为需要时,之前的导出Excel为html方式的方法就受阻了,于是,需要开始新的百度与google来解决问题. 前提为OLEDB+Excel. 根据需求,多数是对于表的数据的导入.于是 ...
- eclipse注册码生成,在eclipse3.3.x上测试可用
这段时间刚加入一个新的项目组,项目组使用的Flex框架. 开发工具由项目组统一提供,使用的是Eclipse 3.3.0,里面包含了其他开发人员集成上去的许多插件,个人感觉比较实用.但是这个版本Ecli ...
- Normalize [ 浏览器渲染格式化 ]
/*! normalize.css v3.0.2 | MIT License | git.io/normalize */ /** * 1. Set default font family to san ...
- 详解null
前言 在java中初始化的时候经常用到null,也经常会碰到空指针异常(NullPointerException),由于碰到的频率比较高,我认为有必要进行一下了解,揭开它的神秘面纱. 一.null是代 ...
- 集成产品开发-IPD简介
内训IPD流程,听完后,觉的流程的力量很强大,可以高效的团队几千上万人的研发团队,来正确地为同一个目标前进.因为讲解者是从华为出来的,所以,相关的案例分析以及理解,都是以华为研发为模板来讲解的.这没错 ...
- ZigBee绑定细节
ZigBee中的绑定由APS层来管理,除了绑定表管理外,APS层还有组表管理.快速地址查找等服务功能.应用层不能直接调用APS层中的数据服务来传输数据,只能通过AF层封装的AD_DataRequest ...
- hash桶
#include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...