Decide Whether an Integer Is a Sum of Two Squares

Quick Overview

Decide Whether an Integer Is a Sum of Two Squares. Return whether nonnegative c equals a × a + b × b for some integers a,b.

Decide Whether an Integer Is a Sum of Two Squares

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return whether nonnegative c equals a × a + b × b for some integers a,b. Zero is allowed for either integer. ```cpp class Solution { public: bool judgeSquareSum(int c); }; ``` ### Examples **Example 1:** ```text c = 25 Output: true Explanation: 3*3+4*4 = 25. ``` **Example 2:** ```text c = 7 Output: false Explanation: Squares no larger than 7 are 0,1,4; no pair sums to 7. ``` ### Constraints - The integer c lies in 0..2147483647. ### Clarifying Questions Allow zero and keep squared arithmetic wide enough at the upper boundary. ```hint Read the boundary condition How does the sum change as either nonnegative candidate magnitude increases? ``` ### Contract considerations Allow zero and keep squared arithmetic wide enough at the upper boundary.

Overview: Decide Whether an Integer Is a Sum of Two Squares. Return whether nonnegative c equals a × a + b × b for some integers a,b.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether nonnegative c equals a × a + b × b for some integers a,b. Zero is allowed for either integer.

class Solution {
public:
    bool judgeSquareSum(int c);
};

Examples

Example 1:

c = 25
Output: true
Explanation: 3*3+4*4 = 25.

Example 2:

c = 7
Output: false
Explanation: Squares no larger than 7 are 0,1,4; no pair sums to 7.

Constraints

  • The integer c lies in 0..2147483647.

Clarifying Questions Guidance

Allow zero and keep squared arithmetic wide enough at the upper boundary.

Contract considerations

Allow zero and keep squared arithmetic wide enough at the upper boundary.