
- 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 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 Articles
- Tuple with the same Product in C++
- Python – Filter tuple with all same elements
- Find whether all tuple have same length in Python
- Program to find numbers with same consecutive differences in Python
- C++ program to find two numbers with sum and product both same as N
- Program to find maximum length of subarray with positive product in Python
- Cummulative Nested Tuple Column Product in Python
- Find two numbers with sum and product both same as N in C++ Program
- Kth Column Product in Tuple List in Python
- Python Program to find the largest element in a tuple
- Program to find maximum subarray min-product in Python
- Python program to Find the size of a Tuple
- Python program to find hash from a given tuple
- 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