Pregunta de entrevista de Two Sigma

some bfs dfs question with math and binary search