Skip to content

Commit 97a28cb

Browse files
committed
Added the Solution for Tuple With Same Product
1 parent 8f897e7 commit 97a28cb

File tree

2 files changed

+253
-1
lines changed

2 files changed

+253
-1
lines changed

dsa-problems/leetcode-problems/1700-1799.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const problems = [
170170
"problemName": "1726. Tuple with Same Product",
171171
"difficulty": "Medium",
172172
"leetCodeLink": "https://leetcode.com/problems/tuple-with-same-product",
173-
"solutionLink": "#"
173+
"solutionLink": "/dsa-solutions/lc-solutions/1700-1799/tuple-with-same-product"
174174
},
175175
{
176176
"problemName": "1727. Largest Submatrix With Rearrangements",
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
---
2+
id: tuple-with-same-product
3+
title: Tuple with Same Product
4+
sidebar_label: 1726 - Tuple with Same Product
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Counting
9+
10+
description: "This is a solution to theTuple with Same Product problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
Given an array nums of distinct positive integers, return 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 a != b != c != d.
15+
16+
### Examples
17+
18+
**Example 1:**
19+
```
20+
Input: nums = [2,3,4,6]
21+
Output: 8
22+
Explanation: There are 8 valid tuples:
23+
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
24+
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
25+
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: nums = [1,2,4,5,10]
32+
Output: 16
33+
Explanation: There are 16 valid tuples:
34+
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
35+
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
36+
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
37+
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
38+
```
39+
40+
41+
### Constraints
42+
- `1 <= nums.length <= 1000`
43+
- `1 <= nums[i] <= 10^4`
44+
- `All elements in nums are distinct.`
45+
46+
## Solution for Path With Minimum Effort Problem
47+
### Approach
48+
#### Brute Force -
49+
- The first approach that is find all sets of a , b ,c , d by using 4 nested loop . Time Complexity of this approach is very high $ O(N^4)$ which is not feasible
50+
#### Optimized Approach Using Hashmap
51+
52+
##### Calculate Product Pairs:
53+
54+
- Iterate through each possible pair of elements in the array.
55+
- For each pair (nums[i], nums[j]), calculate their product.
56+
- Store the product in a hash map (mp) where the key is the product and the value is the count of pairs that produce this product.
57+
##### Count Tuples:
58+
- Iterate through the hash map.
59+
- For each unique product that appears n times, the number of ways to choose 2 pairs from n pairs is given by the combination formula C(n, 2) = n * (n - 1) / 2.
60+
- Since each valid tuple of pairs can be permuted in 8 different ways (each pair can be swapped with the other), multiply the result by 8.
61+
##### Return the Result:
62+
- Sum up the counts from all unique products to get the total number of valid tuples and multiply by 8.
63+
64+
<Tabs>
65+
<TabItem value="Solution" label="Solution">
66+
67+
#### Implementation
68+
```jsx live
69+
function Solution(arr) {
70+
var tupleSameProduct = function(nums) {
71+
let mp = new Map();
72+
73+
for (let i = 0; i < nums.length; ++i) {
74+
for (let j = i + 1; j < nums.length; ++j) {
75+
let product = nums[i] * nums[j];
76+
mp.set(product, (mp.get(product) || 0) + 1);
77+
}
78+
}
79+
80+
let ans = 0;
81+
82+
for (let count of mp.values()) {
83+
ans += (count * (count - 1)) / 2;
84+
}
85+
86+
return ans * 8;
87+
};
88+
const input = [2,3,4,6]
89+
const output = tupleSameProduct(input)
90+
return (
91+
<div>
92+
<p>
93+
<b>Input: </b>
94+
{JSON.stringify(input)}
95+
</p>
96+
<p>
97+
<b>Output:</b> {output.toString()}
98+
</p>
99+
</div>
100+
);
101+
}
102+
```
103+
104+
#### Complexity Analysis
105+
106+
- Time Complexity: $ O(N^2) $
107+
- Space Complexity: $ O(N^2)$
108+
109+
## Code in Different Languages
110+
<Tabs>
111+
<TabItem value="JavaScript" label="JavaScript">
112+
<SolutionAuthor name="@hiteshgahanolia"/>
113+
```javascript
114+
var tupleSameProduct = function(nums) {
115+
let mp = new Map();
116+
117+
for (let i = 0; i < nums.length; ++i) {
118+
for (let j = i + 1; j < nums.length; ++j) {
119+
let product = nums[i] * nums[j];
120+
mp.set(product, (mp.get(product) || 0) + 1);
121+
}
122+
}
123+
124+
let ans = 0;
125+
126+
for (let count of mp.values()) {
127+
ans += (count * (count - 1)) / 2;
128+
}
129+
130+
return ans * 8;
131+
};
132+
```
133+
134+
</TabItem>
135+
<TabItem value="TypeScript" label="TypeScript">
136+
<SolutionAuthor name="@hiteshgahanolia"/>
137+
```typescript
138+
function tupleSameProduct(nums: number[]): number {
139+
let mp = new Map<number, number>();
140+
141+
for (let i = 0; i < nums.length; ++i) {
142+
for (let j = i + 1; j < nums.length; ++j) {
143+
let product = nums[i] * nums[j];
144+
mp.set(product, (mp.get(product) || 0) + 1);
145+
}
146+
}
147+
148+
let ans = 0;
149+
150+
for (let count of mp.values()) {
151+
ans += (count * (count - 1)) / 2;
152+
}
153+
154+
return ans * 8;
155+
}
156+
157+
```
158+
</TabItem>
159+
<TabItem value="Python" label="Python">
160+
<SolutionAuthor name="@hiteshgahanolia"/>
161+
```python
162+
from collections import defaultdict
163+
164+
class Solution:
165+
def tupleSameProduct(self, nums: list[int]) -> int:
166+
mp = defaultdict(int)
167+
168+
for i in range(len(nums)):
169+
for j in range(i + 1, len(nums)):
170+
product = nums[i] * nums[j]
171+
mp[product] += 1
172+
173+
ans = 0
174+
175+
for count in mp.values():
176+
ans += (count * (count - 1)) // 2
177+
178+
return ans * 8
179+
180+
```
181+
182+
</TabItem>
183+
<TabItem value="Java" label="Java">
184+
<SolutionAuthor name="@hiteshgahanolia"/>
185+
```java
186+
import java.util.HashMap;
187+
import java.util.Map;
188+
189+
public class Solution {
190+
public int tupleSameProduct(int[] nums) {
191+
Map<Integer, Integer> mp = new HashMap<>();
192+
193+
for (int i = 0; i < nums.length; ++i) {
194+
for (int j = i + 1; j < nums.length; ++j) {
195+
int product = nums[i] * nums[j];
196+
mp.put(product, mp.getOrDefault(product, 0) + 1);
197+
}
198+
}
199+
200+
int ans = 0;
201+
202+
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
203+
int n = entry.getValue();
204+
ans += (n * (n - 1)) / 2;
205+
}
206+
207+
return ans * 8;
208+
}
209+
}
210+
211+
```
212+
213+
</TabItem>
214+
<TabItem value="C++" label="C++">
215+
<SolutionAuthor name="@hiteshgahanolia"/>
216+
```cpp
217+
class Solution {
218+
public:
219+
int tupleSameProduct(vector<int>& nums) {
220+
unordered_map<int, int> mp;
221+
222+
for (int i = 0; i < nums.size(); ++i) {
223+
for (int j = i + 1; j < nums.size(); ++j) {
224+
int product = nums[i] * nums[j];
225+
mp[product]++;
226+
}
227+
}
228+
229+
int ans = 0;
230+
231+
for (auto it = mp.begin(); it != mp.end(); ++it) {
232+
int n = it->second;
233+
ans += (n * (n - 1)) / 2; // total tuples
234+
}
235+
236+
return ans * 8;
237+
}
238+
};
239+
240+
```
241+
</TabItem>
242+
</Tabs>
243+
244+
</TabItem>
245+
</Tabs>
246+
247+
## References
248+
249+
- **LeetCode Problem**: [Tuple With Same Product](https://leetcode.com/problems/tuple-with-same-product/description/)
250+
251+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/tuple-with-same-product/solutions)
252+

0 commit comments

Comments
 (0)