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).
Input: N = 2
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
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:
- 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.
- 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:
Time Complexity: O(N2 log N)
Auxiliary Space: O(N)
- 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 email@example.com. 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