Sum of Square Numbers - Problem

Given a non-negative integer c, determine whether there exist two integers a and b such that a² + b² = c.

Return true if such integers exist, otherwise return false.

Note: Both a and b can be zero or negative, but for this problem we only consider non-negative values where a ≥ 0 and b ≥ 0.

Input & Output

Example 1 — Basic True Case
$ Input: c = 5
Output: true
💡 Note: 1² + 2² = 1 + 4 = 5, so there exist integers a=1 and b=2 such that a² + b² = c
Example 2 — Perfect Square
$ Input: c = 4
Output: true
💡 Note: 0² + 2² = 0 + 4 = 4 (or 2² + 0² = 4), so it can be expressed as sum of two squares
Example 3 — Impossible Case
$ Input: c = 3
Output: false
💡 Note: No combination of two squares equals 3: 0²+0²=0, 0²+1²=1, 1²+1²=2, 0²+2²=4. None equal 3.

Constraints

  • 0 ≤ c ≤ 2³¹ - 1

Visualization

Tap to expand
Sum of Square Numbers: c = 5Inputc = 5Find a,b wherea² + b² = 51² + 2² = 5 ✓OutputtrueCheck all possible combinations: 0²+?², 1²+?², 2²+?²Found: 1² + 2² = 1 + 4 = 5
Understanding the Visualization
1
Input
Given integer c = 5
2
Process
Find if c can be written as a² + b²
3
Output
Return true if possible, false otherwise
Key Takeaway
🎯 Key Insight: Only check values up to √c since larger values would make the sum exceed c
Asked in
Facebook 25 Google 20 Microsoft 15
42.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen