# Count of quadruples with product of a pair equal to the product of the remaining pair

• Difficulty Level : Medium
• 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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count the number of``// unique quadruples from an array``// that satisfies the given condition``void` `sameProductQuadruples(``int` `nums[],``                           ``int` `N)``{``    ``// Hashmap to store``    ``// the product of pairs``    ``unordered_map<``int``, ``int``> umap;` `    ``// Store the count of``    ``// required quadruples``    ``int` `res = 0;` `    ``// Traverse the array arr[] and``    ``// generate all possible pairs``    ``for` `(``int` `i = 0; i < N; ++i) {``        ``for` `(``int` `j = i + 1; j < N; ++j) {` `            ``// Store their product``            ``int` `prod = nums[i] * nums[j];` `            ``// Pair(a, b) can be used to``            ``// generate 8 unique permutations``            ``// with another pair (c, d)``            ``res += 8 * umap[prod];` `            ``// Increment um[prod] by 1``            ``++umap[prod];``        ``}``    ``}` `    ``// Print the result``    ``cout << res;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 6 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``sameProductQuadruples(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;``class` `GFG``{``  ` `// Function to count the number of``// unique quadruples from an array``// that satisfies the given condition``static` `void` `sameProductQuadruples(``int``[] nums,``                           ``int` `N)``{``    ``// Hashmap to store``    ``// the product of pairs``    ``int``[] umap = ``new` `int``[``10000``];``    ` `    ``// Store the count of``    ``// required quadruples``    ``int` `res = ``0``;` `    ``// Traverse the array arr[] and``    ``// generate all possible pairs``    ``for` `(``int` `i = ``0``; i < N; ++i)``    ``{``        ``for` `(``int` `j = i + ``1``; j < N; ++j)``        ``{` `            ``// Store their product``            ``int` `prod = nums[i] * nums[j];` `            ``// Pair(a, b) can be used to``            ``// generate 8 unique permutations``            ``// with another pair (c, d)``            ``res += ``8` `* umap[prod];` `            ``// Increment um[prod] by 1``            ``++umap[prod];``        ``}``    ``}` `    ``// Print the result``     ``System.out.println(res);``}`  `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] arr = { ``2``, ``3``, ``4``, ``6` `};``    ``int` `N = arr.length;` `    ``sameProductQuadruples(arr, N);``}``}` `// This code is contributed by code_hunt.`

## Python3

 `# Python program for the above approach` `# Function to count the number of``# unique quadruples from an array``# that satisfies the given condition``def` `sameProductQuadruples(nums, N) :` `    ``# Hashmap to store``    ``# the product of pairs``    ``umap ``=` `{};` `    ``# Store the count of``    ``# required quadruples``    ``res ``=` `0``;` `    ``# Traverse the array arr[] and``    ``# generate all possible pairs``    ``for` `i ``in` `range``(N) :``        ``for` `j ``in` `range``(i ``+` `1``, N) :` `            ``# Store their product``            ``prod ``=` `nums[i] ``*` `nums[j];   ``            ``if` `prod ``in` `umap :``                ` `                ``# Pair(a, b) can be used to``                ``# generate 8 unique permutations``                ``# with another pair (c, d)``                ``res ``+``=` `8` `*` `umap[prod];``    ` `                ``# Increment umap[prod] by 1``                ``umap[prod] ``+``=` `1``;``            ``else``:``                ``umap[prod] ``=` `1` `    ``# Print the result``    ``print``(res);` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``2``, ``3``, ``4``, ``6` `];``    ``N ``=` `len``(arr);` `    ``sameProductQuadruples(arr, N);` `    ``# This code is contributed by AnkThon`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG``{` `// Function to count the number of``// unique quadruples from an array``// that satisfies the given condition``static` `void` `sameProductQuadruples(``int``[] nums,``                           ``int` `N)``{``    ``// Hashmap to store``    ``// the product of pairs``    ``int``[] umap = ``new` `int``[10000];``    ` `    ``// Store the count of``    ``// required quadruples``    ``int` `res = 0;` `    ``// Traverse the array arr[] and``    ``// generate all possible pairs``    ``for` `(``int` `i = 0; i < N; ++i)``    ``{``        ``for` `(``int` `j = i + 1; j < N; ++j)``        ``{` `            ``// Store their product``            ``int` `prod = nums[i] * nums[j];` `            ``// Pair(a, b) can be used to``            ``// generate 8 unique permutations``            ``// with another pair (c, d)``            ``res += 8 * umap[prod];` `            ``// Increment um[prod] by 1``            ``++umap[prod];``        ``}``    ``}` `    ``// Print the result``    ``Console.Write(res);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int``[] arr = { 2, 3, 4, 6 };``    ``int` `N = arr.Length;` `    ``sameProductQuadruples(arr, N);``}``}` `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output:

`8`

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

My Personal Notes arrow_drop_up