Sqrt(x)
Problem Statement
Section titled “Problem Statement”Given a non-negative integer x, return the square root of x rounded down to the nearest integer.
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Sqrt(x)
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Binary Search | O(n) | O(1) | When applicable |
| Newton | O(n) | O(1) | When applicable |
| Iterative | O(n) | O(1) | When applicable |
Approach 1: Binary Search
Section titled “Approach 1: Binary Search”This approach provides an efficient solution for sqrt(x).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”def sqrtx_binary_search(x: int) -> int: """Find integer square root using binary search.""" if x < 2: return x
left, right = 2, x // 2 while left <= right: mid = (left + right) // 2 if mid * mid == x: return mid elif mid * mid < x: left = mid + 1 else: right = mid - 1
return right
# Test casesprint(sqrtx_binary_search(4)) # 2print(sqrtx_binary_search(8)) # 2using namespace std;
int sqrtxBinarySearch(int x) { if (x < 2) return x;
long long left = 2, right = x / 2; while (left <= right) { long long mid = (left + right) / 2; if (mid * mid == x) { return mid; } else if (mid * mid < x) { left = mid + 1; } else { right = mid - 1; } }
return right;}
int main() { cout << sqrtxBinarySearch(4) << endl; // 2 cout << sqrtxBinarySearch(8) << endl; // 2 return 0;}public class SqrtxBinarySearch { public static int sqrtxBinarySearch(int x) { if (x < 2) return x;
long left = 2, right = x / 2; while (left <= right) { long mid = (left + right) / 2; if (mid * mid == x) { return (int) mid; } else if (mid * mid < x) { left = mid + 1; } else { right = mid - 1; } }
return (int) right; }
public static void main(String[] args) { System.out.println(sqrtxBinarySearch(4)); // 2 System.out.println(sqrtxBinarySearch(8)); // 2 }}function sqrtxBinarySearch(x) { if (x < 2) return x;
let left = 2, right = Math.floor(x / 2); while (left <= right) { const mid = Math.floor((left + right) / 2); if (mid * mid === x) { return mid; } else if (mid * mid < x) { left = mid + 1; } else { right = mid - 1; } }
return right;}
console.log(sqrtxBinarySearch(4)); // 2console.log(sqrtxBinarySearch(8)); // 2fn sqrtx_binary_search(x: i32) -> i32 { if x < 2 { return x; }
let mut left = 2i64; let mut right = (x / 2) as i64;
while left <= right { let mid = (left + right) / 2; if mid * mid == x as i64 { return mid as i32; } else if mid * mid < x as i64 { left = mid + 1; } else { right = mid - 1; } }
right as i32}
fn main() { println!("{}", sqrtx_binary_search(4)); // 2 println!("{}", sqrtx_binary_search(8)); // 2}package main
import "fmt"
func sqrtxBinarySearch(x int) int { if x < 2 { return x }
left, right := 2, x/2 for left <= right { mid := (left + right) / 2 if mid*mid == x { return mid } else if mid*mid < x { left = mid + 1 } else { right = mid - 1 } }
return right}
func main() { fmt.Println(sqrtxBinarySearch(4)) // 2 fmt.Println(sqrtxBinarySearch(8)) // 2}Approach 2: Newton
Section titled “Approach 2: Newton”This approach provides an efficient solution for sqrt(x).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": pass# Java Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: sqrtx# Approach: newton
# Implementation placeholder for sqrtx_newton# This file is auto-generated and should contain the solution code.
def sqrtx_newton(): pass
if __name__ == "__main__": passApproach 3: Iterative
Section titled “Approach 3: Iterative”This approach provides an efficient solution for sqrt(x).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass# Java Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: sqrtx# Approach: iterative
# Implementation placeholder for sqrtx_iterative# This file is auto-generated and should contain the solution code.
def sqrtx_iterative(): pass
if __name__ == "__main__": pass