File tree 1 file changed +32
-0
lines changed
1726-TuplewithSameProduct
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ impl Solution {
4
+ pub fn tuple_same_product ( nums : Vec < i32 > ) -> i32 {
5
+ let n = nums. len ( ) ;
6
+ let mut processed_products = HashSet :: with_capacity ( ( n - 3 ) * ( n + 1 ) / 2 ) ;
7
+ let nums_set = nums. iter ( ) . copied ( ) . collect :: < HashSet < _ > > ( ) ;
8
+ let mut combinations = 0 ;
9
+ for ( i, & num1) in nums. iter ( ) . take ( n-3 ) . enumerate ( ) {
10
+ for & num2 in nums. iter ( ) . skip ( i + 1 ) {
11
+ let product = num1 * num2;
12
+ if processed_products. contains ( & product) {
13
+ continue ;
14
+ }
15
+ let mut multipliers = 0 ;
16
+ for & maybe_multiplier in nums. iter ( ) . skip ( i) {
17
+ if product % maybe_multiplier == 0 {
18
+ let second_multiplier = product / maybe_multiplier;
19
+ if second_multiplier != maybe_multiplier && nums_set. contains ( & ( product / maybe_multiplier) ) {
20
+ multipliers += 1 ;
21
+ }
22
+ }
23
+ }
24
+ if multipliers > 3 {
25
+ combinations += multipliers * ( ( multipliers - 2 ) )
26
+ }
27
+ processed_products. insert ( product) ;
28
+ }
29
+ }
30
+ combinations
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments