↓Skip to main content
  1. LeetCode/

LeetCode 633: Sum of Square Numbers

·2 mins· ·
LeetCode Medium Two-Pointers Math Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-25

  • Submissions: Wrong Answer ×3 → Accepted ×2

Approach
#

Find non-negative integers a, b with a^2 + b^2 = c.

Converge a from 0 and b from ⌊√c⌋:

  • a^2 + b^2 > c: b is too big, b -= 1.
  • a^2 + b^2 < c: a is too small, a += 1.
  • Equal: found.

Nothing is missed: each step rules out “the current b is too big with every value >= a” or “the current a is too small with every value <= b”, exactly like converging pointers in Two Sum II.

My Attempts
#

All three WAs were boundary issues:

VersionProblem
Attempt 1Started a at 1, missing 0. c = 4 = 0^2 + 2^2 and c = 0 both returned False
Attempt 2Started a at 0 but looped while a < b, missing a == b. c = 2 = 1^2 + 1^2 returned False; it also started b at c, taking O(c) steps
Attempt 3b = c ** 0.5 is a float, and squaring it back is inexact. For c = 2, (√2)^2 evaluates to 2.0000000000000004, which is greater than 2, so nothing matched and it returned False

int(c ** 0.5) passed. math.isqrt is safer still, since it never touches floating point.

Solution
#

from math import isqrt

class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        a, b = 0, isqrt(c)
        while a <= b:
            s = a * a + b * b
            if s == c:
                return True
            if s < c:
                a += 1
            else:
                b -= 1
        return False

Complexity
#

Time O(√c), space O(1).

Mistakes to Avoid
#

  • Forgetting that 0 is a square.
  • Looping while a < b and missing a == b.
  • Squaring a floating-point square root and comparing exactly.

Interview Explanation
#

I converge a from 0 and b from the integer square root of c: if the sum of squares is too big, decrease b; if too small, increase a; if equal, done. The boundaries matter: 0 counts, a may equal b, and isqrt avoids floating-point error. Time O(√c).

Related

LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 1712: Ways to Split Array Into Three Subarrays
·4 mins
LeetCode Medium Binary-Search Prefix-Sum Two-Pointers
LeetCode 1712 note: fix the first cut and find the valid range of the second cut with two bisects or two monotonic pointers
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 189: Rotate Array
·2 mins
LeetCode Medium Array Math Two-Pointers
LeetCode 189 note: take k %= n, then reverse three times: the whole array, the first k, and the last n - k, with O(1) extra space