- 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

# Program to find tuple with same product in Python

Suppose we have an array nums with unique positive values, we have to find the number of tuples (a, b, c, d) such that a*b = c*d where a, b, c, and d are elements of nums, and all elements a, b, c and d are distinct.

So, if the input is like nums = [2,3,4,6], then the output will be 8 because we can get tuples like (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), (4,3,6,2).

To solve this, we will follow these steps −

- dic := an empty map, default value is 0 if some key is not present
- ans:= 0
- for i in range 0 to size of nums - 2, do
- for j in range i+1 to size of nums, do
- dic[nums[i]*nums[j]] := dic[nums[i]*nums[j]] + 1

- for j in range i+1 to size of nums, do
- for each v in list of all values of dic, do
- if v is same as 1, then
- go for next iteration

- v:= v-1
- s:= (v/2) * (8+8*v)
- ans := ans + s

- if v is same as 1, then
- return ans as integer

## Example

Let us see the following implementation to get better understanding −

from collections import defaultdict def solve(nums): dic = defaultdict(int) ans=0 for i in range(len(nums)-1): for j in range(i+1,len(nums)): dic[nums[i]*nums[j]]+=1 for v in dic.values(): if v==1: continue v=v-1 s=(v/2) * (8+8*v) ans+=s return int(ans) nums = [3,4,6,2] print(solve(nums))

## Input

[3,4,6,2]

## Output

0

- Related Questions & Answers
- Tuple with the same Product in C++
- Find whether all tuple have same length in Python
- Program to find numbers with same consecutive differences in Python
- Python – Filter tuple with all same elements
- C++ program to find two numbers with sum and product both same as N
- Find two numbers with sum and product both same as N in C++ Program
- Cummulative Nested Tuple Column Product in Python
- Program to find maximum length of subarray with positive product in Python
- Program to find maximum subarray min-product in Python
- Kth Column Product in Tuple List in Python
- Python program to Find the size of a Tuple
- Python program to find hash from a given tuple
- Python program to find Cartesian product of two lists
- Program to find number of quadruples for which product of first and last pairs are same in Python
- Program to find maximum product of contiguous subarray in Python

Advertisements