Possible Duplicate:

finding all numbers less than x in a BST

How would I modify a binary search to find the number of numbers in a sorted array that are less than a certain number?

If you have an already sorted array of numbers simply find the insertion point for your item in the sorted array usng a binary search algorithm. The index of the insertion point gives you the number of elements that are less than your target number.

In your comments you raised two good questions:

- What if the number is not in the list?

To handle this you keep searching until you find the point where the number should be if it were present, that is the index where the current element is greater than x and the previous element is less than x.

- What if there are duplicates?

To handle this, instead of stopping when you first find an element, continue searching until you lower bound and upper bound meet. If you hit a value that is equal to x treat it in the same way as if you found a number that was too high and continue bisecting.

Binary search for the largest number lower than your given number. Once you have its position, that position also directly relates to the count you're interested in.

This pseudocode should get you on the right track, but I haven't tested it. `k`

= given number

```
while left < right
mid = (left + right) / 2
if arr[mid] >= k // too big, not interested
right = mid;
else // maybe too small, try bigger values
left = mid + 1
right - 1 or left - 1 (they're equal) is the position you're after.
```

For example: `8 11 13 20 50`

, `k = 19`

```
left = 1, right = 5
mid = 3
arr[3] = 13 < 19 => left = 4
left = 4, right = 5
mid = 4
arr[4] = 20 >= 19 => right = 4
left >= right => left - 1 = 4 - 1 = 3 is the position you're after
```

Return all numbers less than the index of the value returned by the binary search.

Find number and check index.

Since this is specifically an array of numbers and not just comparable objects, you might want to look at interpolation search. If the numbers are uniformly distributed, it can find the index in O(log log n) time instead of O(log n).

Similar Questions

This is exactly the same question as How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger?, except it applies to char[] rather than byte[]:

This question already has an answer here: Find a pair of elements from an array whose sum equals a given number 15 answers I recently came across an interesting Java Algorithm statement Given a

I need to write a program to find the Max product of three numbers for a given array of size N. Is there any effective algorithm for this? I just need to know the algorithm steps. Non of algorithms th

I am using C# regex library to do some text find and replace. I would like to change the following: 1 -> one 11 -> one one 123 -> one two three for example, here's my code to replace ampersa

This question already has an answer here: C++ What variable type for extremely big integer numbers? [duplicate] 1 answer If all types of values (ints, floats, etc.) have limitations, how do pro

This is an algorithm optimization problem. I have an integer array A and want to build array B such that B[i] contains index j of the element in A[j] such that (B[i] = j) 1. j > i 2. A[j] < A[i

I have an array like this one: int[] numbers = new [] { 1, 2, 3, 4 }; I'd like to randomize this (different each time) so that it makes another array with the same size and numbers but in a different

Possible Duplicate: Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? Let's say a[] is a sorted vector. How do I find the first(sm

Suppose I have an array of numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 How do I construct a 3x3 matrix where the first row contains 1,2,3; the second row contains 4,5,6; the third row contains 7, 8, 9? Essen

Possible Duplicate: Find the Smallest Integer Not in a List I got asked this question in an interview Given an unsorted array, find the smallest number that is missing. Assume that all numbers are p

I need to find numbers from a string. How does one find numbers from a string in VBA Excel?

Hi I am using sql db2 to create age_bands as so: case when age between 0 and 1 then '0-1' when age between 2 and 3 then '2-3' . . . when age between 10 and 11 then '10-11' which of course when you or

Given a 2D array, for example: 0 0 0 0 0 0 2 3 0 1 0 8 5 0 7 7 0 0 0 4 Output should be groups of clusters: Cluster 1: <2,3,8,5,7> Cluster 2: <1,7,4>

I need to have objects sorted by price (decimal) value for fast access. I need to be able to find all objects with price more then A or less than B. I was thinkg about SortedList, but it does not prov

This question already has an answer here: How to check any missing number from a series of numbers? 6 answers I got table1 that contains sequential number in range between 1 - 100 and I want to

I had to make a histogram of a given array, the frequency of the numbers in the array should be marked with *. My program works, the problem is that the array isn't sorted correctly if there is a nega

Possible Duplicate: Generating random numbers in Javascript Hi.. I want to generate random numbers (integers) in javascript within a specified range. ie. 101-999. How can I do that. Does Math.random

I have a code. My question would be: why don't I get even_numbers0,even_numbers1,even_numbers2..? Or odd_numbers0, odd_numbers1, odd_numbers2, odd_numbers3...? #include <iostream> #include <i

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array. I know how to create the program using array. int large

I want to generate eight bit (uint8_t) random numbers so that I exclude a set of well known numbers which have already been specified. Basically numbers from 0x00 - 0xFF but I have certain numbers wit

How do I recursively remove files that are less than 1MB in size from a directory?

This question already has an answer here: How to find the kth smallest element in the union of two sorted arrays? 9 answers I was asked this question in an interview. I was able to do it in O(n

I'm trying to create a matrix of random double numbers. The matrix must be of size n x n and all numbers must be between 1 and 100. I've been trying to sort it out for ages now and I know it must be s

I was asked the following question in my interview yesterday: Consider a Java or C++ array say X which is sorted and no two elements in it are same. How best can you find an index say i such that elem

Trying to find a more efficient algorithm to find pairs of numbers whose product is less than the given input. Tried using the following : k = N - 1; while(k>0) { div = N/k; if(N%k==0) div--; ans+

If I have a sorted array, how do I find the sequential numbers? Btw, this is for determining if a poker hand is a straight or not. Duplicates in the array have been removed. I can do this, but it woul

Is there a way to find n fibonacci numbers starting from a given k? I know that the basic method would be to find all fibonacci numbers starting from 0, keep track of when a number in the series is gr

I have a large (12 digit) BCD number, encoded in an array of 6 bytes - each nibble is one BCD digit. I need to multiply it by 10^x, where x can be positive or negative. I know it can be done by shift

Imagine you have a list of N numbers. You also have the target number. You want to find the combination of Z numbers that summed together are close to the target. Example: Target = 3.085 List = [0.8

Given a sorted array a[0...n-1], find all pairs of numbers whose sum is less than S. Is there an O(n) solution?

I have to create an array size of 10 and generate random numbers from 0 to 100 including 0 and excluding 100. When I write the code it keeps giving me an error of: Exception in thread main java.lan

You are given 2^32-2 unique numbers that range from 1 to 2^32-1. It's impossible to fit all the numbers into memory (thus sorting is not an option). You are asked to find the missing number. What woul

Does somebody know the command to make less display line numbers in the left column?

i have an array full of numbers. i need to find the maximum different between 2 numbers but the biggest number is before the smallest number in the array. public static int maximalDrop (int [] a) For

I need help formatting numbers in a specific way. If a number has three decimal places or less, I would like it to remain the same. If a number has more than three significant figures, I would like

Given an array of unsorted positive ints, write a function that finds runs of 3 consecutive numbers (ascending or descending) and returns the indices where such runs begin. If no such runs are found,

I am trying to populate an array of four elements with integers that are less than 9. Here is my code: generated_number=Array.new(4)#create empty array of size 4 generated_number.each do |random| #for

I have been trying to generate a matrix that is size 4x5 and full of random numbers by doing the following (in ruby 1.8.7): m_rand = Matrix #create an empty matrix n = 0 for n in 0...5 m=0 for m in 0.

Today, an interviewer asked me this question. My immediate response was that we could simply do a linear search, comparing the current element with the previous element in the array. He then asked me

i have two variables like: a=200 b=205 and want to find out all numbers between these two numbers (including. these specified numbers).

How to restrict a JTextFiled to accept only numbers less then 10, no words, no spaces or any other special characters?

whats the logic for calculating HCF of given numbers?

Is there a way to display the contact names instead of the contact numbers? This method returns the phone numbers within an inbox, but I need the names instead: UPDATE: code changed from the original

Having a Binary Search Tree of int create a linked list of all the integers less than a given integer x value. What I've tried? 1)brutal solution(inefficient) An inorder visit of the BST, I insert a

I have n numbers. n <= 1000000. Each number will be positive integer and less than 10^9. It is sure that there will be only one number will occur once, rest will occur twice or even number of times

Inside a MySQL database I store sets of numbers giving only the first and the last one e.g.: id | from | to 1 | 65.789 | 66.323 2 | 66.151 | 69.298 etc... I try to figure out a way with PHP and MySQ

Given an array of numbers, I would like to create a number identifier that represents that combination as unique as possible. For example: int[] inputNumbers = { 543, 134, 998 }; int identifier = crea

How do I get string using NSScanner from a string which contains string as well as numbers too? i.e. 001234852ACDSB The result should be 001234852 and ACDSB I am able to get numbers from the string u

Possible Duplicates: what is the most efficient way to calculate the least common multiple of two integers Least common multiple for 3 or more numbers Whats the simple logic for calculating LCM of g

I came across this question in a coding competition- You're given an array of positive integers and are allowed to change the sign of any of the integers whenever you require. Write a program to calcu