For each x in 0..N compute rem = N*N - x*x. If rem is a perfect square y*y then (x,y) is a valid solution. To count unique unordered pairs (treat (x,y) same as (y,x)), only accept the solution when x <= y — this ensures each unordered pair is counted once. Use an integer square-root (isqrt) to avoid floating-point rounding issues.
classSolution {
public:int countPairs(int n) {
int N = n;
int target = N * N;
int ans =0;
for (int x =0; x <= N; ++x) {
int rem = target - x * x;
if (rem <0) break;
int y = (int)std::floor(std::sqrt((double)rem));
if (y * y == rem && x <= y) ++ans; // only count when x<=y to get unordered pairs
}
return ans;
}
};
classSolution {
publicintcountPairs(int n) {
int N = n;
int target = N * N;
int ans = 0;
for (int x = 0; x <= N; ++x) {
int rem = target - x * x;
if (rem < 0) break;
int y = (int)Math.floor(Math.sqrt(rem));
if (y * y == rem && x <= y) ++ans;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funcountPairs(n: Int): Int {
val N = n
val target = N * N
var ans = 0var x = 0while (x <= N) {
val rem = target - x * x
if (rem < 0) breakval y = Math.floor(Math.sqrt(rem.toDouble())).toInt()
if (y * y == rem && x <= y) ans++ x++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defcountPairs(self, n: int) -> int:
import math
target = n * n
ans =0for x in range(n +1):
rem = target - x * x
if rem <0:
break y = math.isqrt(rem)
if y * y == rem and x <= y: # enforce x<=y to count unordered pairs once ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pubstructSolution;
impl Solution {
pubfncountPairs(&self, n: i32) -> i32 {
let N = n;
let target = N * N;
letmut ans: i32=0;
for x in0..=N {
let rem = target - x * x;
if rem <0 { break; }
let y = (rem asf64).sqrt().floor() asi32;
if y * y == rem && x <= y { ans +=1; }
}
ans
}
}
Think of the sequence of squares 0^2, 1^2, …, N^2. Use two indices i (low) and j (high) starting at 0 and N. At any time the sum s = i*i + j*j moves monotonically as you shift pointers: increasing i increases s, decreasing j decreases s. This monotonicity lets you find all pairs in linear time without sqrt.
To count unique unordered pairs, maintain the invariant i <= j and only count a solution when i <= j is true (the loop condition enforces this). When a match is found, increment i and decrement j to skip the symmetric counterpart (j,i) and avoid double-counting. If i == j and i*i + j*j == target, that corresponds to a pair where both elements are equal (only possible when N^2 is twice a square) and should be counted once.
classSolution {
public:int countPairs(int N) {
int ans =0;
int i =0, j = N;
int target = N * N;
while (i <= j) {
int s = i * i + j * j;
if (s == target) { ++ans; ++i; --j; } // move both pointers to avoid double-count
elseif (s < target) ++i;
else--j;
}
return ans; // counts unordered pairs with i <= j
}
};
classSolution {
publicintcountPairs(int N) {
int ans = 0;
int i = 0, j = N;
int target = N * N;
while (i <= j) {
int s = i * i + j * j;
if (s == target) { ++ans; ++i; --j; }
elseif (s < target) ++i;
else--j;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funcountPairs(N: Int): Int {
var ans = 0var i = 0var j = N
val target = N * N
while (i <= j) {
val s = i * i + j * j
if (s == target) { ans++; i++; j-- }
elseif (s < target) i++else j-- }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defcountPairs(self, N: int) -> int:
ans =0 i, j =0, N
target = N * N
while i <= j:
s = i * i + j * j
if s == target:
ans +=1 i +=1 j -=1elif s < target:
i +=1else:
j -=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pubstructSolution;
impl Solution {
pubfncountPairs(&self, N: i32) -> i32 {
letmut ans: i32=0;
letmut i: i32=0;
letmut j: i32= N;
let target: i32= N * N;
while i <= j {
let s = i * i + j * j;
if s == target { ans +=1; i +=1; j -=1; }
elseif s < target { i +=1; }
else { j -=1; }
}
ans
}
}
Number-theory remark: an integer m is representable as sum of two squares iff every prime factor p ≡ 3 (mod 4) appears with an even exponent in m’s prime factorization. For m = N^2 this condition always holds, so representations exist; counting distinct representations requires Gaussian-integer factorization and is beyond this note’s scope.