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

Given a number N, the task is to find the number of quadraples such that a2 + b2 = c2 + d2 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
Output 28
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 a2 + b2 = c2 + d2



Time Complexity: O(N4)
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 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 concluded 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:

  1. Traverse over all the pairs(say (a, b)) over [1, N] and store the value of a2 + b2 with it’s occurence in a Map.
  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.
  3. Print the count.

Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

6

Time Complexity: O(N2 log N)
Auxiliary Space: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : offbeat