I recorded a 30-minute lecture about binary search. It should allow you to really understand it and stop guessing about +1 in random places. Enjoy.

https://www.youtube.com/watch?v=GU7DpgHINWQ

Consider watching with captions on and with x1.25 speed.

# | User | Rating |
---|---|---|

1 | tourist | 3686 |

2 | mnbvmar | 3509 |

3 | Benq | 3339 |

4 | LHiC | 3330 |

5 | wxhtxdy | 3329 |

6 | sunset | 3279 |

7 | Petr | 3275 |

7 | V--o_o--V | 3275 |

9 | yutaka1999 | 3190 |

10 | ecnerwala | 3153 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 190 |

2 | Radewoosh | 189 |

3 | rng_58 | 165 |

4 | PikMike | 160 |

5 | Vovuh | 158 |

6 | Petr | 155 |

7 | 300iq | 154 |

8 | majk | 149 |

9 | Um_nik | 148 |

10 | Swistakk | 144 |

I recorded a 30-minute lecture about binary search. It should allow you to really understand it and stop guessing about +1 in random places. Enjoy.

https://www.youtube.com/watch?v=GU7DpgHINWQ

Consider watching with captions on and with x1.25 speed.

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/16/2019 09:33:02 (f2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

ETA — Blogewoosh #8?

Thank you, but you didn't mention invariants.

I think I explained how to approach BS problems with the logic behind that. How would invariants help more?

Please tell how to approach for problems such as https://codeforces.com/contest/1169/problem/C

Code: 54682960

Observe that the problem is basically asking for the most number of times that a number has to change.

The problem asks to find the minimum number of operations, which is bounded from 1 to m. Thus, you can binary search for the minimum number of operations in O(log(m)) time.

That means that you can dedicate O(N) time to checking if it is possible to sort the array using <= a certain amount of operations.

Why you don't do analysis of all next educational rounds problems? They are cool to learn topics but sometimes they are hard to understand even with the editorial

edu rounds r too mathematical. its looks like author took the problems from some IMO book.

I am just going to leave this here.

Hahaha

I didn't knew Errichto was that old, how many years is he in cp?

Can you make a similar video on topics like RMQ and Geometry as many people struggle in those topics?

Finally 1st.

In case of finding maximum in first increasing and then decreasing array if duplicate elements are present how to handle this?

You can't apply binary search in an array like $$$[1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]$$$. You can't say which side (left or right) to choose when you ask about the middle element.

The only exception is that the maximum can be repeated multiple times — if you hit it at least once, you found a correct answer anyway.

Thanks.

Really great tutorial , I suggest making such tutorials in number theory topics like segmented sieve or search techniques like ternary search.

I have also implemented the code for the problem to find the maximum value in a mountain range . this is my code , can you kindly review my code if there is any bugs ?

https://ideone.com/5YgGqF

You should compare with adjacent elements, not with maximum so far. Plus your example sequence isn't increasing-decreasing.

Just solve Leetcode problems from links under the video.

Thank you Errichto for the great lecture.

However I'm concerned about binary search on double:

I think previous code won't stop, as when L and R get near the sqrt (which is 1000000000), numbers will be large so precision will be low and comparision (R — L > 1e-9) will never be false.

I prefer use L and Sz (size of the interval), instead of L and R as Sz will always get halved each time, so it's guaranteed to stop.

We can also limit the iterations by log of the number.

The thing is it's impossible to get precision $$$10^{-9}$$$ for that big numbers anyway. The precision of your code is for sure bigger than $$$10^{-9}$$$. In a problem from a contest, either values will be smaller or they will ask for relative or absolute error and then we should check that instead of just an absolute error.

Hi Errichto, When there will be a lecture on dp?