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

时间: 2024-11-09 14:12:57

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

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ? k ? BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

Binary Search 专栏

Binary Search 时间复杂度 O(logN ), 因为每次减少一半 相当于取log Q: 什么时候可以用Binary Seach? A: 当数据是Sorted 并且支持Random Access的时候 Binary Search 的基本规则 1. 搜索空间在循环中不断减小 The Searching Area decrease during the process 2. 目标元素(如果存在)不可以被排除到搜索空间之外 Basic Binary Search public int bin

First Occurrence Of Binary Search

和普通的binary search不同就是在处理找到等于target的数 首先要记录这个结果,然后我们现在其实是需要向左找,右边的数都不重要,因为我们要找最左出现的,我们就假设现在的情况是nums[mid] > target,所以high = mid - 1.如果找不到了,那么之前的结果就是最左侧的,否则就继续找. 1 public int firstOccurence(int[] nums, int target) { 2 int left = 0; 3 int right = nums.le

【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 a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a t

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

[LeetCode] Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

12. binary search Trees

12. binary search Trees? ? The search tree data structure supports many dynamic-set operations,including search ,minimum,maximum,predecessor,successor ,insert ,and delete.? Thus, we can use a search tree both as a dictionary and as a priority queue.

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 element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod