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.,

.**Examples:**

Input:N = 20Output:4Explanation:

The 4 possible pairs are (10, 2), (-10, 2), (-10, -2) and (10, -2).Input:N = 80Output:12Explanation:

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).

**Approach:**

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:

=>

and

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:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the integral` `// solutions of the given equation` `void` `findSolutions(` `int` `N)` `{` ` ` `// Initialise count to 0` ` ` `int` `count = 0;` ` ` `// Iterate till sqrt(N)` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(N); i++) {` ` ` `if` `(N % i == 0) {` ` ` `// If divisor's pair sum is even` ` ` `if` `((i + N / i) % 2 == 0) {` ` ` `count++;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the total possible solutions` ` ` `cout << 4 * count << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given number N` ` ` `int` `N = 80;` ` ` `// Function Call` ` ` `findSolutions(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` `// Function to find the integral ` `// solutions of the given equation ` `static` `void` `findSolutions(` `int` `N) ` `{ ` ` ` `// Initialise count to 0 ` ` ` `int` `count = ` `0` `; ` ` ` `// Iterate till sqrt(N) ` ` ` `for` `(` `int` `i = ` `1` `; i <= Math.sqrt(N); i++)` ` ` `{ ` ` ` `if` `(N % i == ` `0` `)` ` ` `{ ` ` ` ` ` `// If divisor's pair sum is even ` ` ` `if` `((i + N / i) % ` `2` `== ` `0` `)` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the total possible solutions ` ` ` `System.out.print(` `4` `* count);` `} ` `// Driver code` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given number N ` ` ` `int` `N = ` `80` `; ` ` ` ` ` `// Function Call ` ` ` `findSolutions(N); ` `} ` `} ` `// This code is contributed by Shubham Prakash. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `import` `math;` `# Function to find the integral` `# solutions of the given equation` `def` `findSolutions(N):` ` ` `# Initialise count to 0` ` ` `count ` `=` `0` `;` ` ` `# Iterate till sqrt(N)` ` ` `for` `i ` `in` `range` `(` `1` `, ` `int` `(math.sqrt(N)) ` `+` `1` `):` ` ` `if` `(N ` `%` `i ` `=` `=` `0` `):` ` ` `# If divisor's pair sum is even` ` ` `if` `((i ` `+` `N ` `/` `/` `i) ` `%` `2` `=` `=` `0` `):` ` ` `count ` `+` `=` `1` `;` ` ` ` ` `# Print the total possible solutions` ` ` `print` `(` `4` `*` `count);` `# Driver Code` `# Given number N` `N ` `=` `80` `;` `# Function Call` `findSolutions(N);` `# This code is contributed by Code_Mech` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System;` `class` `GFG{ ` `// Function to find the integral ` `// solutions of the given equation ` `static` `void` `findSolutions(` `int` `N) ` `{ ` ` ` `// Initialise count to 0 ` ` ` `int` `count = 0; ` ` ` `// Iterate till sqrt(N) ` ` ` `for` `(` `int` `i = 1; i <= Math.Sqrt(N); i++)` ` ` `{ ` ` ` `if` `(N % i == 0)` ` ` `{ ` ` ` ` ` `// If divisor's pair sum is even ` ` ` `if` `((i + N / i) % 2 == 0)` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the total possible solutions ` ` ` `Console.Write(4 * count);` `} ` `// Driver code` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given number N ` ` ` `int` `N = 80; ` ` ` ` ` `// Function Call ` ` ` `findSolutions(N); ` `} ` `} ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

12

**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.

## Recommended Posts:

- 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
- Count pairs of characters in a string whose ASCII value difference is K
- 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
- Check if N can be represented as sum of squares of two consecutive integers
- Count numbers whose difference with N is equal to XOR with N
- Count of integers in an Array whose length is a multiple of K
- 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
- Minimum number of squares whose sum equals to given number n
- 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
- Count the pairs in an array such that the difference between them and their indices is equal
- 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

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.