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 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking all possible pairs of numbers. For each pair, we'll calculate the sum of their squares and see if it matches the given target number. It's like trying every combination until we find one that works.
Here's how the algorithm would work step-by-step:
def sum_of_square_numbers_brute_force(target_number):
first_number = 0
while first_number <= target_number:
second_number = 0
while second_number <= target_number:
sum_of_squares = (first_number * first_number) + (second_number * second_number)
if sum_of_squares == target_number:
return True
second_number += 1
# Increment first number to check
# against all possible second numbers
first_number += 1
# If no match is found, return false
# after exhausting all combinations
return FalseThe problem asks us to determine if a given number can be expressed as the sum of two perfect squares. Instead of checking every possible combination, we'll use a two-pointer approach, starting from opposite ends, to efficiently search for a valid pair.
Here's how the algorithm would work step-by-step:
def sum_of_square_numbers(target):
left_number = 0
right_number = int(target**0.5)
while left_number <= right_number:
sum_of_squares = left_number**2 + right_number**2
if sum_of_squares == target:
return True
# Need a larger sum, increment
if sum_of_squares < target:
left_number += 1
# Need a smaller sum, decrement
else:
right_number -= 1
# Exhausted all possibilities
return False| Case | How to Handle |
|---|---|
| c = 0 | Handles this correctly as a=0, b=0 is a valid solution; solution should return true. |
| c = 1 | Handles this correctly as a=1, b=0 is a valid solution (or a=0, b=1); solution should return true. |
| c is a large perfect square (e.g., c = 2147483649 = 46341^2) | The square root calculation should be performed using a double to avoid potential integer overflow, and then convert to long for further calculations to avoid integer overflow on squaring. |
| c is a number that requires a large 'a' or 'b' to be a valid solution but 'a' and 'b' still fit in integer | The solution should correctly handle cases where a and b are close to the square root of c and within the integer range. |
| Integer overflow in a*a or b*b | Use long data type for a and b to prevent overflow in a*a + b*b, or perform sqrt operation on doubles which is safe. |
| c is negative | The problem states c is non-negative, but ideally, the code should return false since squares are always non-negative. |
| No valid integer solution exists | The algorithm must correctly identify when no such 'a' and 'b' exist and return false, such as c = 3. |
| Floating point precision errors during square root calculation | Avoid using == for checking perfect square of sqrt(c), instead use a tolerance or compare squares directly, ensuring a and b are integers after casting sqrt(c). |