Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
Example 1:
Input: c = 5 Output: true Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3 Output: false
Constraints:
0 <= c <= 231 - 1Problem summary: Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Two Pointers · Binary Search
5
3
valid-perfect-square)sum-of-squares-of-special-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #633: Sum of Square Numbers
class Solution {
public boolean judgeSquareSum(int c) {
long a = 0, b = (long) Math.sqrt(c);
while (a <= b) {
long s = a * a + b * b;
if (s == c) {
return true;
}
if (s < c) {
++a;
} else {
--b;
}
}
return false;
}
}
// Accepted solution for LeetCode #633: Sum of Square Numbers
func judgeSquareSum(c int) bool {
a, b := 0, int(math.Sqrt(float64(c)))
for a <= b {
s := a*a + b*b
if s == c {
return true
}
if s < c {
a++
} else {
b--
}
}
return false
}
# Accepted solution for LeetCode #633: Sum of Square Numbers
class Solution:
def judgeSquareSum(self, c: int) -> bool:
a, b = 0, int(sqrt(c))
while a <= b:
s = a**2 + b**2
if s == c:
return True
if s < c:
a += 1
else:
b -= 1
return False
// Accepted solution for LeetCode #633: Sum of Square Numbers
use std::cmp::Ordering;
impl Solution {
pub fn judge_square_sum(c: i32) -> bool {
let mut a: i64 = 0;
let mut b: i64 = (c as f64).sqrt() as i64;
while a <= b {
let s = a * a + b * b;
match s.cmp(&(c as i64)) {
Ordering::Equal => {
return true;
}
Ordering::Less => {
a += 1;
}
Ordering::Greater => {
b -= 1;
}
}
}
false
}
}
// Accepted solution for LeetCode #633: Sum of Square Numbers
function judgeSquareSum(c: number): boolean {
let [a, b] = [0, Math.floor(Math.sqrt(c))];
while (a <= b) {
const s = a * a + b * b;
if (s === c) {
return true;
}
if (s < c) {
++a;
} else {
--b;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.