2Brute2Best
🔥0

Sqrt(x)

Easy·LeetCode #69 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a non-negative integer x. Return its square root rounded down to a whole number.

In other words, return the biggest integer r such that r · r ≤ x.

You may not use any built-in power or square-root function, such as pow(x, 0.5) or sqrt(x).

Input

A single integer x.

Output

The largest integer r with r · r ≤ x.

Example 1
Input: x = 4
Output: 2

2 · 2 = 4, and 3 · 3 = 9 is too big.

Example 2
Input: x = 8
Output: 2

The real square root is 2.828…, which rounds down to 2. Check: 2 · 2 = 4 ≤ 8, but 3 · 3 = 9 > 8.

Constraints
  • • 0 ≤ x ≤ 2^31 − 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“rounded down / largest r with r·r ≤ x”
→ "Largest value such that a condition holds" → find the last Y in Y…YN…N.
“no array, just a number x”
→ The search space is the range of possible answers, [0, x].
“don't use sqrt or pow”
→ You must find the answer with integer comparisons only.
“x up to 2^31 − 1”
→ mid · mid overflows int → use long long or compare mid ≤ x / mid.
03

Approach Complexity Ladder

Brute Force — count up from 0O(√x)O(1)

Try r = 0, 1, 2, … until (r + 1)² is bigger than x

Optimal — binary search on the answer rangeO(log x)O(1)

Binary search r in [0, x] for the last r with r·r ≤ x

Brute Force Intuition

Start with r = 0. While the next number r + 1 still has a square ≤ x, step up. When (r + 1)² is too big, r is the answer.

Recognition rule for this sub-pattern:

Sorted array; find a value, or the first/last position where a condition becomes true.
Remember this · Binary Search
Trigger
Sorted data, "minimum X such that…", "maximum X such that…", rotated arrays, answers that are monotonic (if X works, X+1 also works).
Think
Is there a yes/no question whose answers look like NNNNYYYY? Then binary search for the first Y.
Key question
What is my check function, and is it monotonic?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.