Given a number N, the task is to find the number of quadruples such that a2 + b2 = c2 + d2 where (1 <= a, b, c, d <= N).
Example:
Input: N = 2
Output: 6
Explanation:
There are 6 valid quadruples (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
Output 28
Explanation:
There are 28 valid quadruples 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 a2 + b2 = c2 + d2
Time Complexity: O(N4)
Auxiliary Space: O(1)
Efficient approach rather than the Naive approach: The above solution can be reduced in terms of time complexity using some mathematics. Find the quadruplets such that a2 + b2 = c2 + d2.
a2 + b2 = c2 + d2
a2 + b2 - c2 = d2
Taking square root on both sides
(a2 + b2 - c2)1/2 = d
By using the above formula, we can conclude that d exists only if (a2 + b2 - c2)1/2 is a positive integer.
Time Complexity: O(N3)
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 a2 + b2 with its occurrence in a Map.
- Iterate over all the pairs [1, N] and if the sum exists on the map, then count the sum of each pair stored on the map.
- Print the count.
Below is the implementation of the approach:
// 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;
}
// 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
# Python3 program for
# the above approach
from collections import defaultdict
# Function to count
# the quadruples
def countQuadraples(N):
# Counter variable
cnt = 0
# Map to store the
# sum of pair (a^2 + b^2)
m = defaultdict (int)
# Iterate till N
for a in range (1, N + 1):
for b in range (1, N + 1):
# Calculate a^2 + b^2
x = a * a + b * b
# Increment the value in map
m[x] += 1
for c in range (1, N + 1):
for d in range (1, N + 1):
x = c * c + d * d
# Check if this sum
# was also in a^2 + b^2
if x in m:
cnt += m[x]
# Return the count
return cnt
# Driver Code
if __name__ == "__main__":
# Given N
N = 2
# Function Call
print (countQuadraples(N))
# This code is contributed by Chitranayal
// C# program for the above approach
using System;
using System.Collections.Generic;
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)
Dictionary<long,
long> m = new Dictionary<long,
long>();
// 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
if (m.ContainsKey(x))
m[x] = m[x] + 1;
else
m.Add(x, 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[x];
}
}
// Return the count
return cnt;
}
// Driver code
public static void Main(String[] args)
{
// Given N
long N = 2;
// Function call
Console.WriteLine(countQuadraples(N));
}
}
// This code is contributed by Amit Katiyar
<script>
// Javascript program for the above approach
// Function to count the quadruples
function countQuadraples(N)
{
// Counter variable
var cnt = 0;
// Map to store the
// sum of pair (a^2 + b^2)
var m = new Map();
// Iterate till N
for (var a = 1; a <= N; a++) {
for (var b = 1; b <= N; b++) {
// Calculate a^2 + b^2
var x = a * a + b * b;
// Increment the value in map
if(m.has(x))
m.set(x, m.get(x)+1)
else
m.set(x, 1)
}
}
for (var c = 1; c <= N; c++) {
for (var d = 1; d <= N; d++) {
var x = c * c + d * d;
// Check if this sum
// was also in a^2 + b^2
if (m.has(x))
cnt += m.get(x);
}
}
// Return the count
return cnt;
}
// Driver Code
// Given N
var N = 2;
// Function Call
document.write( countQuadraples(N))
</script>
Output:
6
Time Complexity: O(N2 log N)
Auxiliary Space: O(N)