LeetCode Problem

How to Solve Sum of Square Numbers

The problem asks whether two integers, a and b, exist such that their squares sum to a given number, c. By using binary search over the possible values of a and b, we can efficiently check the condition. The approach leverages binary search or two-pointer strategies to find a solution.

GhostInterview Help

Need help with Sum of Square Numbers without spending extra time grinding it?

GhostInterview can read Sum of Square Numbers from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #633Binary search over the valid answer spaceReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Binary search over the valid answer space
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem asks whether two integers, a and b, exist such that their squares sum to a given number, c. By using binary search over the possible values of a and b, we can efficiently check the condition. The approach leverages binary search or two-pointer strategies to find a solution.

Problem Statement

Given a non-negative integer c, decide whether there exist two integers a and b such that a^2 + b^2 = c. The goal is to return true if such integers exist, otherwise false.

For example, for c = 5, the integers a = 1 and b = 2 satisfy the equation 1^2 + 2^2 = 5, so the output would be true. In contrast, for c = 3, no such integers exist, and the output would be false.

Examples

Example 1

Input: c = 5

Output: true

1 * 1 + 2 * 2 = 5

Example 2

Input: c = 3

Output: false

Example details omitted.

Constraints

  • 0 <= c <= 231 - 1

Solution Approach

Binary Search over the answer space

The core approach involves using binary search over the range of potential values for the integers a and b. The idea is to check if the square of one number can be subtracted from c to yield a perfect square, which helps narrow down the possibilities efficiently.

Two Pointers Approach

Another efficient method to solve this problem is by using the two-pointer technique. You can start with one pointer at 0 and the other at sqrt(c), iterating while adjusting the pointers until you either find a match or exhaust the possibilities.

Mathematical Approach

An alternate solution involves mathematical insights into perfect squares. We can check for each candidate square if the remaining difference is also a perfect square using integer square roots.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the approach used. Using binary search, the time complexity is O(sqrt(c)), as we search through potential values of a and b. The space complexity is O(1), as only a few variables are used during execution.

What Interviewers Usually Probe

  • Tests the candidate's ability to apply binary search over a valid range efficiently.
  • Assesses understanding of two-pointer techniques in algorithmic problem-solving.
  • Evaluates the candidate's ability to recognize patterns in math-based problems.

Common Pitfalls or Variants

Common pitfalls

  • Failing to optimize the search space, leading to unnecessarily complex solutions.
  • Overcomplicating the problem and missing simpler solutions like binary search or two-pointer.
  • Not handling edge cases such as c = 0 correctly.

Follow-up variants

  • What if the sum of squares must be divisible by a certain number?
  • What if negative numbers were allowed for a and b?
  • What if the problem asked for more than two squares to sum up to c?

How GhostInterview Helps

  • GhostInterview provides guided approaches, such as binary search, that help solve the problem efficiently.
  • The platform offers detailed breakdowns of potential pitfalls, improving your understanding of two-pointer techniques.
  • With its focus on specific patterns like binary search and math applications, GhostInterview ensures you master solving problems like Sum of Square Numbers.

Topic Pages

FAQ

How can binary search help with the Sum of Square Numbers problem?

Binary search can efficiently narrow down the possible values for the integers a and b by searching the range for valid square sums.

What’s the optimal time complexity for the Sum of Square Numbers problem?

The optimal time complexity is O(sqrt(c)) when using binary search or two-pointer approaches.

Are there any edge cases to consider when solving this problem?

Yes, especially when c is 0, where the solution is trivially true (since 0^2 + 0^2 = 0).

Can two-pointer or binary search be used to solve this problem efficiently?

Yes, both techniques are efficient for this problem. Binary search can be used to explore the possible squares, and two-pointer works well by checking sums incrementally.

How do I optimize my solution for the Sum of Square Numbers problem?

You can optimize your solution by limiting the search space using binary search or the two-pointer technique, both of which offer O(sqrt(c)) time complexity.

GhostInterview Solver

Need direct help with Sum of Square Numbers instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Sum of Square Numbers from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.