The problem requires determining whether there exist two non-negative integers ( a ) and ( b ) such that ( a^2 + b^2 = c ). Given the nature of the problem, a direct iterative approach might be inefficient for large values of ( c ). Instead, leveraging mathematical properties of squares can lead to a more efficient solution.
The chosen approach employs a two-pointer technique combined with mathematical operations:
- Initialization: Start with
left
initialized to 0 andright
initialized to ( \sqrt{c} ) (converted to an integer). This choice ofright
is based on the maximum possible value for ( b ). - Two-pointer Technique: Use a loop where
left
andright
pointers move inward towards each other:- Calculate
sum
as ( \text{left}^2 + \text{right}^2 ). - If
sum
equalsc
, returntrue
as we found ( a ) and ( b ) such that ( a^2 + b^2 = c ). - If
sum
is less thanc
, incrementleft
(left++
). - If
sum
is greater thanc
, decrementright
(right--
).
- Calculate
- Termination: If
left
exceedsright
, exit the loop and returnfalse
, indicating no valid pair ( (a, b) ) exists such that ( a^2 + b^2 = c ).
- Time Complexity: ( O(\sqrt{c}) )
- The main loop runs at most ( \sqrt{c} ) times because
left
starts from 0 andright
starts from ( \sqrt{c} ). Each iteration adjusts eitherleft
orright
, efficiently reducing the search space.
- The main loop runs at most ( \sqrt{c} ) times because
- Space Complexity: ( O(1) )
- The algorithm uses only a constant amount of extra space regardless of the input size ( c ). This includes variables like
left
,right
, andsum
.
- The algorithm uses only a constant amount of extra space regardless of the input size ( c ). This includes variables like
This approach ensures an efficient solution in ( O(\sqrt{c}) ) time, suitable for large values of ( c ), with ( O(1) ) space complexity.