Count quadruplets(A, B, C, D) till N such that sum of square of A and B is equal to that of C and D
Last Updated :
16 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll countQuadraples(ll N)
{
ll cnt = 0;
map<ll, ll> m;
for (ll a = 1; a <= N; a++) {
for (ll b = 1; b <= N; b++) {
ll x = a * a + b * b;
m[x] += 1;
}
}
for (ll c = 1; c <= N; c++) {
for (ll d = 1; d <= N; d++) {
ll x = c * c + d * d;
if (m.find(x) != m.end())
cnt += m[x];
}
}
return cnt;
}
int main()
{
ll N = 2;
cout << countQuadraples(N)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static long countQuadraples( long N)
{
long cnt = 0 ;
Map<Long, Long> m = new HashMap<>();
for ( long a = 1 ; a <= N; a++)
{
for ( long b = 1 ; b <= N; b++)
{
long x = a * a + b * b;
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;
if (m.containsKey(x))
cnt += m.get(x);
}
}
return cnt;
}
public static void main(String[] args)
{
long N = 2 ;
System.out.println(countQuadraples(N));
}
}
|
Python3
from collections import defaultdict
def countQuadraples(N):
cnt = 0
m = defaultdict ( int )
for a in range ( 1 , N + 1 ):
for b in range ( 1 , N + 1 ):
x = a * a + b * b
m[x] + = 1
for c in range ( 1 , N + 1 ):
for d in range ( 1 , N + 1 ):
x = c * c + d * d
if x in m:
cnt + = m[x]
return cnt
if __name__ = = "__main__" :
N = 2
print (countQuadraples(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static long countQuadraples( long N)
{
long cnt = 0;
Dictionary< long ,
long > m = new Dictionary< long ,
long >();
for ( long a = 1; a <= N; a++)
{
for ( long b = 1; b <= N; b++)
{
long x = a * a + b * b;
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;
if (m.ContainsKey(x))
cnt += m[x];
}
}
return cnt;
}
public static void Main(String[] args)
{
long N = 2;
Console.WriteLine(countQuadraples(N));
}
}
|
Javascript
<script>
function countQuadraples(N)
{
var cnt = 0;
var m = new Map();
for ( var a = 1; a <= N; a++) {
for ( var b = 1; b <= N; b++) {
var x = a * a + b * b;
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;
if (m.has(x))
cnt += m.get(x);
}
}
return cnt;
}
var N = 2;
document.write( countQuadraples(N))
</script>
|
Time Complexity: O(N2 log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...