Count of pairs of integers whose difference of squares is equal to N
Given a positive integer N, the task is to find the count of pairs of integers (x, y) whose difference of squares is equal to N, i.e.,
Input: N = 20
The 4 possible pairs are (10, 2), (-10, 2), (-10, -2) and (10, -2).
Input: N = 80
The 12 possible pairs are:
1. (40, 2), (-40, 2), (-40, -2) and (40, -2).
2. (20, 4), (-20, 4), (-20, -4) and (20, -4).
3. (10, 8), (-10, 8), (-10, -8) and (10, -8).
The given equation can also be written as:
Now for an integral solution of the given equation:
is always an integer
are divisors of N
Let (x + y) = p1 and (x + y) = p2
be the two equations where p1 & p2 are the divisors of N
such that p1 * p2 = N.
Solving for the above two equation we have:
From the above calculations, for x and y to be integral, then the sum of divisors must be even. Since there are 4 possible values for two values of x and y as (+x, +y), (+x, -y), (-x, +y) and (-x, -y).
Therefore the total number of possible solution is given by 4*(count pairs of divisors with even sum).
Below is the implementation of the above approach:
Time Complexity: O(sqrt(N))