-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumSwap.js
63 lines (52 loc) · 1.34 KB
/
SumSwap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict'
/*
Given two arrays of integeres, find a pair of values (one value from each array) that you can swap to give the arrays the same sum.
EXAMPLE
Input: [4, 1, 2, 1, 1, 2], [3, 6, 3, 3]
Output: [1, 3]
*/
class SumSwaps{
/**
* @param {number[]} list
* @return {number}
*/
getSum(list){
return list.reduce((a, b) => a + b, 0)
}
/**
* @param {number[]} list1
* @param {number[]} list2
* @return {number[]}
*/
findSwapValues(list1, list2){
let sum1 = this.getSum(list1),
sum2 = this.getSum(list2)
if (sum1 === sum2) return []
let diff = Math.abs(sum1 - sum2) / 2
if (sum1 < sum2){
return this.findswapValuesHelper(list1, list2, diff)
}
else{
return this.findswapValuesHelper(list2, list1, diff)
}
}
/**
* @param {number[]} list1
* @param {number[]} list2
* @param {number} diff
* @return {number[]}
*/
findswapValuesHelper(list1, list2, diff){
let set2 = new Set(list2)
let res = []
list1.some(num => {
let target = num + diff
if (set2.has(target)){
res = [num, target]
return true
}
})
return res
}
}
module.exports = SumSwaps