Given a number **N**, the task is to find the number of quadraples such that **a ^{2} + b^{2} = c^{2} + d^{2}** where (1 <= a, b, c, d <= N).

**Example:**

Input:N = 2

Output:6

Explanation:

There are 6 valid quadraples (1, 1, 1, 1), (1, 2, 1, 2), (1, 2, 2, 1), (2, 1, 1, 2), (2, 1, 2, 1), (2, 2, 2, 2).

Input:N = 4

Output28

Explanation:

There are 28 valid quadraples for N = 4.

**Naive Approach:** The naive approach is to use 4 nested loops in the range **[1, N]** and count those quadruplets **(a, b, c, d)** such that **a ^{2} + b^{2} = c^{2} + d^{2}**

**Time Complexity:** *O(N ^{4})*

**Auxiliary Space:**

*O(1)*

**Efficient approach than Naive approach:** The above solution can be reduced in terms of time complexity using some mathematics. Find the quadruplets such that a^{2} + b^{2} = c^{2} + d^{2}.

a

^{2}+ b^{2}= c^{2}+ d^{2}

a^{2}+ b^{2}– c^{2}= d^{2}

Taking square root on both sides

(a^{2}+ b^{2}– c^{2})^{1/2}= d

By using the above formula, we can concluded that **d** exists only if (a^{2} + b^{2} – c^{2})^{1/2} is a positive integer.

**Time Complexity:** *O(N ^{3})*

**Efficient Approach:** The idea is to use **Hashing**. Below are the steps:

- Traverse over all the pairs(say
**(a, b)**) over**[1, N]**and store the value of**a**with it’s occurence in a Map.^{2}+ b^{2} - Iterate over all the pairs
**[1, N]**and if the sum exists in the map then count the sum of each pairs stored in the map. - Print the count.

Below is the implementation of the approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `typedef` `long` `long` `int` `ll; ` ` ` `// Function to count the quadruples ` `ll countQuadraples(ll N) ` `{ ` ` ` `// Counter variable ` ` ` `ll cnt = 0; ` ` ` ` ` `// Map to store the ` ` ` `// sum of pair (a^2 + b^2) ` ` ` `map<ll, ll> m; ` ` ` ` ` `// Iterate till N ` ` ` `for` `(ll a = 1; a <= N; a++) { ` ` ` ` ` `for` `(ll b = 1; b <= N; b++) { ` ` ` ` ` `// Calculate a^2 + b^2 ` ` ` `ll x = a * a + b * b; ` ` ` ` ` `// Increment the value in map ` ` ` `m[x] += 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(ll c = 1; c <= N; c++) { ` ` ` `for` `(ll d = 1; d <= N; d++) { ` ` ` ` ` `ll x = c * c + d * d; ` ` ` ` ` `// Check if this sum ` ` ` `// was also in a^2 + b^2 ` ` ` `if` `(m.find(x) != m.end()) ` ` ` `cnt += m[x]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given N ` ` ` `ll N = 2; ` ` ` ` ` `// Function Call ` ` ` `cout << countQuadraples(N) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count the quadruples ` `static` `long` `countQuadraples(` `long` `N) ` `{ ` ` ` ` ` `// Counter variable ` ` ` `long` `cnt = ` `0` `; ` ` ` ` ` `// Map to store the ` ` ` `// sum of pair (a^2 + b^2) ` ` ` `Map<Long, Long> m = ` `new` `HashMap<>(); ` ` ` ` ` `// Iterate till N ` ` ` `for` `(` `long` `a = ` `1` `; a <= N; a++) ` ` ` `{ ` ` ` `for` `(` `long` `b = ` `1` `; b <= N; b++) ` ` ` `{ ` ` ` ` ` `// Calculate a^2 + b^2 ` ` ` `long` `x = a * a + b * b; ` ` ` ` ` `// Increment the value in map ` ` ` `m.put(x, m.getOrDefault(x, 0l) + ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(` `long` `c = ` `1` `; c <= N; c++) ` ` ` `{ ` ` ` `for` `(` `long` `d = ` `1` `; d <= N; d++) ` ` ` `{ ` ` ` `long` `x = c * c + d * d; ` ` ` ` ` `// Check if this sum ` ` ` `// was also in a^2 + b^2 ` ` ` `if` `(m.containsKey(x)) ` ` ` `cnt += m.get(x); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given N ` ` ` `long` `N = ` `2` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(countQuadraples(N)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** *O(N ^{2} log N)*

**Auxiliary Space:**

*O(N)*

## Recommended Posts:

- 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
- Count square and non-square numbers before n
- Check if any square (with one colored cell) can be divided into two equal parts
- Count of binary strings of length N having equal count of 0's and 1's
- Count of subarrays whose sum is a perfect square
- Count of pairs in an array whose sum is a perfect square
- Count of square free divisors of a given number
- Count of subarrays having exactly K perfect square numbers
- Count of ways to represent N as sum of a prime number and twice of a square
- Count of subsequences whose product is a difference of square of two integers
- Count of integral coordinates that lies inside a Square
- Count of elements which is product of a pair or an element square
- Count of pairs in an array whose product is a perfect square
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Count of subsets with sum equal to X
- Count subarrays with sum equal to its XOR value
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count of Multiples of A ,B or C less than or equal to N
- Count numbers whose XOR with N is equal to OR with N
- Count non-negative triplets with sum equal to 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.