Count of quadruples with product of a pair equal to the product of the remaining pair
Last Updated :
21 May, 2021
Given an array arr[] of size N, the task is to count the number of unique quadruples (a, b, c, d) from the array such that the product of any pair of elements of the quadruple is equal to the product of the remaining pair of elements.
Examples:
Input: arr[] = {2, 3, 4, 6}
Output: 8
Explanation:
There are 8 quadruples in the array, i.e. (2, 6, 3, 4), (2, 6, 4, 3), (6, 2, 3, 4), (6, 2, 4, 3), (3, 4, 2, 6), (4, 3, 2, 6), (3, 4, 6, 2), and (4, 3, 6, 2), that satisfies the given condition.
Hence, the total number of quadruples is 8.
Input: arr[] = {1, 2, 3, 4}
Output: 0
Explanation: There exists no quadruple satisfying the given condition.
Naive Approach: The simplest approach is to generate all possible quadruples from the given array using four nested loops for every unique quadruple encountered, check if it satisfies the given condition or not. Finally, print the count of such triplets obtained.
Time Complexity: O(N4)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Hashing. To solve this problem, store the product of every distinct pair in the Hashmap M.
Every quadruple (a, b, c, d) satisfying the given condition has 8 permutations:
No of ways of arranging (a, b) = 2 {(a, b), (b, a)}
No of ways of arranging (c, d) = 2 {(c, d), (d, c)}
No of ways of arranging (a, b) and (c, d) = 8 {(a, b, c, d), (a, b, d, c), (b, a, c, d), (b, a, d, c), (c, d, a, b), (c, d, b, a), (d, c, a, b), (d, c, b, a)}.
Hence, the total no of ways = 8.
Follow the steps below to solve the problem:
- Initialize res as 0 to store the count of resultant quadruplets.
- Create an hashmap M to store the frequency of product of distinct pairs in the array.
- Now, generate all possible distinct pairs (arr[i], arr[j]) of the given array and do the following:
- Store the product of arr[i] and arr[j] in a variable prod.
- Add the value of (8*M[prod]) to the variable res.
- Increment the frequency of prod in the hashmap M by 1.
- After the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sameProductQuadruples( int nums[],
int N)
{
unordered_map< int , int > umap;
int res = 0;
for ( int i = 0; i < N; ++i) {
for ( int j = i + 1; j < N; ++j) {
int prod = nums[i] * nums[j];
res += 8 * umap[prod];
++umap[prod];
}
}
cout << res;
}
int main()
{
int arr[] = { 2, 3, 4, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
sameProductQuadruples(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void sameProductQuadruples( int [] nums,
int N)
{
int [] umap = new int [ 10000 ];
int res = 0 ;
for ( int i = 0 ; i < N; ++i)
{
for ( int j = i + 1 ; j < N; ++j)
{
int prod = nums[i] * nums[j];
res += 8 * umap[prod];
++umap[prod];
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 , 6 };
int N = arr.length;
sameProductQuadruples(arr, N);
}
}
|
Python3
def sameProductQuadruples(nums, N) :
umap = {};
res = 0 ;
for i in range (N) :
for j in range (i + 1 , N) :
prod = nums[i] * nums[j];
if prod in umap :
res + = 8 * umap[prod];
umap[prod] + = 1 ;
else :
umap[prod] = 1
print (res);
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 6 ];
N = len (arr);
sameProductQuadruples(arr, N);
|
C#
using System;
class GFG
{
static void sameProductQuadruples( int [] nums,
int N)
{
int [] umap = new int [10000];
int res = 0;
for ( int i = 0; i < N; ++i)
{
for ( int j = i + 1; j < N; ++j)
{
int prod = nums[i] * nums[j];
res += 8 * umap[prod];
++umap[prod];
}
}
Console.Write(res);
}
public static void Main(String[] args)
{
int [] arr = { 2, 3, 4, 6 };
int N = arr.Length;
sameProductQuadruples(arr, N);
}
}
|
Javascript
<script>
function sameProductQuadruples(nums, N) {
var umap = new Array(10000).fill(0);
var res = 0;
for ( var i = 0; i < N; ++i) {
for ( var j = i + 1; j < N; ++j) {
var prod = nums[i] * nums[j];
res += 8 * umap[prod];
++umap[prod];
}
}
document.write(res);
}
var arr = [2, 3, 4, 6];
var N = arr.length;
sameProductQuadruples(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...