Baseline Binary Search · In Search Space O(log x) · O(1)
A datacenter planner receives a floor area in square units and wants the side of the largest square rack cluster that fits exactly or smaller. When the area is not a perfect square, the planner floors the side length. Search the side-length range directly rather than calling a floating-point library routine.
Input: A single long integer x, the area.
Output: floor(sqrt(x)), the largest integer whose square does not exceed x.
1 <= x <= 2^31 - 1The result must be exact integer arithmetic, not floating pointInput: {"x":5}
Output: 2
2 squared is 4, which fits, while 3 squared overshoots 5 - so the floored side is 2.
Input: {"x":4}
Output: 2
The area is a perfect square and the side is exactly 2.