Hello , when solving problems on binary search, i solve them because i know that i'm solving problems on Binary search topic , but if i come to a problem statement i don't know how or even if I should use binary search or not ... so my question is , how to make sure you're solving a binary search problem?








solve more problems, work on more ideas, see the editorial. after some months you will be able to identify it.
You should read about monotonic functions and try to find patterns similar to it. Also finding minimum or maximum value type problems may involve binary search on answer.
Tbh I still couldn't identify Binary search. My entire intuition of bs is "if it looks like dp, but constraints do not support dp and couldn't prove greedy then it must be binary search" and it works almost all the time.
that some crazy identification of bs but IDK i get bs ideas intutively like I solved a problem today I almost instantly recognized it is bs after sorting in non increasing order
If some function is monotonic and you want to know the equal range of some value you should use binary search.
Why would you try to identify a problem is binary search or not? Just be ready to apply binary search whenever you noticed such a function.
I apply binary search when i notice a pattern of TT..TF..FF or F..FTT..T .
i don't get the pattern ...
After a point it is always true or false. You can use BS to find that point... This is a kind of monotonicity pattern mostly used in problems where you need to apply BS on Answer.
One trick you can find in some problems is trying to find a number (often the minimum or the maximum) that works for a certain dataset, and you can verify if a certain number works in O(1) or O(n) (or different depending on the problem and input size).
I'll use this leetcode problem as an example.
You can verify if a certain amount of seconds works with an O(n) operation, and since the function is monotonic, you can simply search for the minimum value that works and return that.
(this doesn't apply to all problems, but it should hopefully be helpful)
Binary Search works when a function $$$f$$$ is increasing/decreasing (sorted in increasing/decreasing way) on $$$\mathbb{R}$$$ in general case , so if you've a computational complexity of $$$\mathcal{O}(f')$$$ for $$$f$$$ then you can apply binary search on $$$\mathcal{O}(f' \times \log(|r-l|)$$$ that's we apply binary search on range $$$[l,r]$$$.
However in some case this defintion can be optimized by some optimizations such as precalculation , for example :
Given an array $$$a$$$ of $$$a_1,a_2,..,a_n$$$ , find any subarray of sum $$$x$$$
Do prefix sum , prefix sum is increasing , thus you can for each index search for index such
Complexity is
Example 2