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))
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.