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 91753

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

2 votes

Find the largest possible number not larger than some integer N and is the product of K cons...

It can be checked easily whether the product of the first $K$ primes is larger than $N$. If yes, there is no such number that is not larger than $N$ and is the product of $K$ consecutive primes. Other …
John L.'s user avatar
  • 39.1k
3 votes
Accepted

How can I apply binary search to find two adjacent increasing elements in an unsorted array?

You are almost there. "If yes then an index $i$ that satisfies $a[i]<a[i+1]$, will surely appear on the right side"; so continue searching on the right side. If no, continue searching on the left side …
John L.'s user avatar
  • 39.1k
17 votes
Accepted

"Guess the number" Problem on Turing machines

Yes, it is pointless and absurd to implement an algorithm to "guess the number" using the most common kind of Turing machine, whose head can read any cell on the tape, since, as you pointed out, there …
John L.'s user avatar
  • 39.1k
3 votes
Accepted

Binary Search return value

There are multiple ways to handle the case when the key(the searched value) is not found by a binary search. Or any search on elements accessible by indices. What you see is the Java approach, the app …
John L.'s user avatar
  • 39.1k
2 votes
Accepted

Determine whether a sorted array contain at least 4 distinct elements in O(log n) time

Here is a cleaner and better way to solve the problem. # Return the smallest index where the element is bigger than `A[start_index]`. # If `len(A)` is returned, no element is bigger than `A[start_inde …
John L.'s user avatar
  • 39.1k
2 votes
Accepted

Error in pivot selection algorithm for merge phase [Sorting]

The "Algorithm 1 Pivot selection" of that paper is rather sloppy. The critical mistake is, as you have noted, "the while loop in the algorithm is exited too early due to the stride being reduced down …
John L.'s user avatar
  • 39.1k
4 votes
Accepted

Binary-ish search through partially ordered set

Here is a simple algorithm that runs in $O(N^2)$ time and $O(N)$ space, assuming that $f(\emptyset)$, $f(\{1\})$, $f(\{2\})$, $\cdots$, $f(\{N\})$ are given in an array. The starting idea is about th …
John L.'s user avatar
  • 39.1k
1 vote

Find the number using binary search against one possible lie

A similar problem, named "Black Hole", appears as one of the problems of 2019 Russian Olympiad of schoolchildren in computer science. The problem asks for a program that interacts with a jury program …
John L.'s user avatar
  • 39.1k
3 votes
Accepted

Binary search uneven split number of queries?

The number of queries in the question is assumed to mean the exact upper bound of the number of queries in the worst case. So, only the worst case is considered except in exercise 3. That is, the wan …
John L.'s user avatar
  • 39.1k
2 votes
Accepted

How can I understand a hint for finding the lowest height of the last bulb in a Garland?

Short answer Let $d=h_1-h_2$. Do a binary search for the largest value of $d$ in interval $[0,A]$ so that none of $n$ bulbs in the garland will be lower than the ground. The wanted minimum value of t …
John L.'s user avatar
  • 39.1k
1 vote
Accepted

An algorithm that find the max X/Y in a polygon in O(log n)

Assume the polygon is a convex polygon; otherwise there might not be an $O(\log n)$ algorithm to find the maximum of X-coordinates or Y-coordinates. First, let us handle the case of X-coordinate. Le …
John L.'s user avatar
  • 39.1k
2 votes

Given a sorted array with n elements and element x that is inside the array at position k, f...

You can apply binary expanding search starting from both ends of the given array. For ease of statement, I will assume $n=2^k$ for some integer $k\ge0$. Otherwise, a boundary check can be included in …
John L.'s user avatar
  • 39.1k