Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Binary Search/Binary Search

Technique

Ternary Search (unimodal optimisation)

Minimise or maximise a unimodal function by discarding a third of the range per step (or binary search on f(m+1) - f(m) for integers).

Learn firstBinary Search
0%
mastery
0/6 solved
Practise nextFind in Mountain Array
IdeaPracticePitfalls

Recognise it

The signal

cost is convex in a single parameter

  1. 1Brute force

    Evaluate every candidate.

  2. 2The observation

    Linear scan of the parameter: unimodality means comparing two probes discards a side.

  3. 3Why it works

    The optimum always lies within [lo, hi].

Reach for it when

  • +meeting point that minimises max/sum distance
  • +real-valued optimum

Not when

  • ×function has plateaus (strict unimodality violated)
  • ×multiple local optima

Practice

Next

Find in Mountain Array

0/6 solved
  • Meeting on the LineCodeforces1600
  • Ruler (hard version)Codeforces1700
  • Find in Mountain ArrayHard
  • Restorer DistanceCodeforces2100
  • Minimizing DifferenceCodeforces2000
  • Gift SetCodeforces2100

Pitfalls

Misconceptions

  • !using ternary search when the predicate is monotone (plain binary search is enough)
Go deeperresources

Resources

  • Binary searchcp-algorithms · article
  • Binary SearchUSACO Guide · course
  • ITMO Binary Search courseCodeforces EDU · course
  • Binary Search explore cardLeetCode · course
  • Sorting and searching cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems