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 the pairs in an array such that the difference between them and their indices is equal
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Find two numbers such that difference of their squares equal to N
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Subsets of size K with product equal to difference of two perfect squares
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count pairs with set bits sum equal to K
- Count pairs from two arrays having sum equal to K
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count pairs in a binary tree whose sum is equal to a given value x
- Count all distinct pairs with product equal to K
- Count elements in an Array that can be represented as difference of two perfect squares
- Count of index pairs with equal elements in an array
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count pairs of natural numbers with GCD equal to given number
- Count of pairs having each element equal to index of the other from an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.