Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 683

Questions about the binary search algorithm, which can be used to find elements of an ordered list in O(log n) time.

0 votes

Order of storage of pointers for a linked list of length n

You have just described the order. First you include the middle of the list. Then the middles of the two halves. And so on. That's the same storage convention as a heap: the children of node number $x …
Yuval Filmus's user avatar
5 votes

Categorization of Binary search as Divide and Conquer

There is no accepted formal definition of the divide and conquer paradigm (see this question for some suggestions), and so we must regard this paradigm as an informal concept. The main idea in divide …
Yuval Filmus's user avatar
0 votes
Accepted

Smallest segment after whose removal all elements are distinct

The idea is that you can check whether the answer is at most some value $m$ in time $O(n)$. Applying binary search on $m$, you obtain an $O(n\log n)$ solution. How do you check whether the answer is …
Yuval Filmus's user avatar
2 votes
Accepted

How can we prove that in binary search, low – high ≤ 1

Let's denote the indices by $l,h,m$. If $l \leq h$ and $h-l$ is even then $m = \frac{l+h}{2}$ and so in the following iteration, the new values $l',h'$ will either be $\frac{l+h}{2}+1,h$ or $l,\frac{l …
Yuval Filmus's user avatar
2 votes
Accepted

Finding pair of sum in sorted array in time complexity less than $O(n)$

Your interviewer is wrong. Suppose that you had an algorithm which gets as input a sorted integer array $A_1,\ldots,A_n$, outputs Yes iff $A_i + A_j = 0$ for some $i \neq j$, and always accesses at mo …
Yuval Filmus's user avatar
3 votes
Accepted

How to analyze/test a binary search algorithm?

First, of all, the worst-case running time of binary search is $\Theta(\log n)$. What you are looking for is the average running time of binary search under some reasonable random model, for example t …
Yuval Filmus's user avatar
2 votes

Finding the number of $L\leq j\leq R$ such that $a[j] \leq a[i]$

Here is a solution in time $O((Q+n)\log^2 n)$ and space $O(n\log n)$. For simplicity, assume that $n$ is a power of $2$, and that the array is zero-based. A dyadic interval is an interval of the form …
Yuval Filmus's user avatar
3 votes

Extend binary search

Your problem is known as group testing. An essentially optimal algorithm for your problem is Generalized binary splitting. There are also pretty good non-adaptive algorithms (such algorithms fix all q …
Yuval Filmus's user avatar
4 votes
Accepted

"Unbounded" binary search in $\log_2(n) + O(?)$ comparisons

Your question was completely solved by Bentley and Yao, An almost optimal algorithm for unbounded sorting, and Beigel, Unbounded searching algorithms. They showed that if $f\colon \mathbb{N} \to \math …
Yuval Filmus's user avatar
1 vote
Accepted

Time complexity of binary search

Let me start with the first question. Since $\lfloor x \rfloor \leq x$, $$ \left\lfloor \frac{\mathit{high}+\mathit{low}}{2} \right\rfloor - \mathit{low} \leq \frac{\mathit{high}+\mathit{low}}{2} - \m …
Yuval Filmus's user avatar
4 votes

Searching for a string of numbers in a large digit sequence

Use your web browser. You can get the first 2 million or so digits in this link, and then use your browser's searching facilities. I was able to locate 351814 within the first 2 million digits (though …
Yuval Filmus's user avatar
1 vote
Accepted

Finding Triples that satisfy modulo equation in $O(n\log n)$ time

Hint: Use FFT to multiply the polynomials $$ \sum_a x^{a^2 \pmod{n}}, \qquad \sum_b x^{b^3 \pmod{n}}. $$
Yuval Filmus's user avatar
3 votes
Accepted

lower bound for Renyi–Ulam Game with lies

For the first question, suppose that you have an algorithm that supports one error, and uses $m$ questions. For each element $i \in \{1,\ldots,n\}$, let $x_{i0}$ be the vector containing the answers t …
Yuval Filmus's user avatar
2 votes
Accepted

Efficient search algorithm for a monotonic boolean array wherein the probability of target's...

If you knew the distribution of $n^*$, you could find an optimal binary decision tree (there are several $O(n\log n)$ algorithms) in terms of expected number of queries (you can't beat binary search i …
Yuval Filmus's user avatar
4 votes

Understanding Binary Search for Kth Smallest element in an Array

Let me start by recalling the question: Given an unsorted array of $n$ elements in the range $1,\ldots,m$, find the $k$th smallest element in $O(n\log m)$ time and $O(1)$ case. Calling the array …
Yuval Filmus's user avatar

15 30 50 per page