- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums, with unique positive numbers nums. We have to find the number of quadruples like (a, b, c, d) from nums such that a*b = c*d, a, b, c and d all are distinct elements of nums.

So, if the input is like nums = [3, 6, 4, 8], then the output will be 8, because the quadruples are [[3,8,6,4], [3,8,4,6], [8,3,6,4], [8,3,4,6], [6,4,3,8], [4,6,3,8], [6,4,8,3], [4,6,8,3]].

To solve this, we will follow these steps −

- c := a new map
- n := size of nums
- for i in range 0 to n - 1, do
- for j in range i + 1 to n - 1, do
- x := nums[i] * nums[j]
- c[x] := 1 + (c[x] if available, otherwise 0)

- for j in range i + 1 to n - 1, do
- ret := 0
- for each x in list of all values in c, do
- ret := ret + x *(x - 1)

- return ret * 4

Let us see the following implementation to get better understanding −

def solve(nums): c = {} n = len(nums) for i in range(n): for j in range(i + 1, n): x = nums[i] * nums[j] c[x] = c.get(x, 0) + 1 ret = 0 for x in c.values(): ret += x * (x - 1) return ret * 4 nums = [3, 6, 4, 8] print(solve(nums))

[3, 6, 4, 8]

8

- Related Questions & Answers
- Program to find number of operations needed to make pairs from first and last side are with same sum in Python
- Find the Number of Maximum Product Quadruples in C++
- Program to find a sublist where first and last values are same in Python
- Program to count index pairs for which array elements are same in Python
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python
- Program to find number of good pairs in Python
- Program to find number of restricted paths from first to last node in Python
- Program to find all words which share same first letters in Python
- Program to find tuple with same product in Python
- Find the sum of first and last digit for a number using C language
- Program to find max number of K-sum pairs in Python
- Python Program for Product of unique prime factors of a number
- Program to find out the number of pairs of equal substrings in Python
- Program to find number of good leaf nodes pairs using Python

Advertisements