
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of quadruples for which product of first and last pairs are same in Python
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
Example
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))
Input
[3, 6, 4, 8]
Output
8
- Related Articles
- Program to find number of operations needed to make pairs from first and last side are with same sum in Python
- Program to find a sublist where first and last values are same in Python
- Find the Number of Maximum Product Quadruples in C++
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- Program to count index pairs for which array elements are same in Python
- C++ program to find number of l-r pairs for which XORed results same as summation
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python
- Program to find number of restricted paths from first to last node in Python
- Program to find number of good pairs in Python
- Program to find all words which share same first letters in Python
- Program to find max number of K-sum pairs in Python
- Program to find tuple with same product in Python
- Program to find out the number of pairs of equal substrings in Python
- Program to find number of pairs between x, whose multiplication is x and they are coprime in Python
- Program to find number of good leaf nodes pairs using Python

Advertisements