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
(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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Count pairs in a binary tree whose sum is equal to a given value x
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of subsequences whose product is a difference of square of two integers
- Split N into two integers whose addition to A and B makes them equal
- Check if N can be represented as sum of squares of two consecutive integers
- Find two numbers such that difference of their squares equal to N
- Subsets of size K with product equal to difference of two perfect squares
- Count pairs of characters in a string whose ASCII value difference is K
- Count of integers in an Array whose length is a multiple of K
- Largest sub-array whose all elements are perfect squares
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Count numbers whose difference with N is equal to XOR with N
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.