Skip to content

Commit a4b01e0

Browse files
committed
Done with maximum pair sum.
1 parent d7ae0de commit a4b01e0

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

list/max-pair-sum/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 23/01/2020
4+
*/
5+
6+
const MaxPairNumberSum = (arr) => {
7+
/*
8+
* Steps:
9+
* 1. Define, 2. Represent, 3. Approach/Strategy, 4. Algorithm, 5. Analyse, 6. Experiment
10+
*/
11+
// ([51, 71, 17, 42])).toEqual([51, 42])
12+
// pick pairs, calculate digits, compare pairs
13+
let i = 0;
14+
let max = 0;
15+
let pair = [];
16+
while(i < (arr.length - 1)) {
17+
let j = i + 1;
18+
let jLength = arr.slice(j);
19+
while(j < jLength.length) {
20+
let iItem = arr[i];
21+
let jItem = jLength[j];
22+
let addIDigit = (iItem).toString().split('').reduce((a, b) => a+Number(b), 0);
23+
let addJDigit = (jItem).toString().split('').reduce((a, b) => a+Number(b), 0);
24+
if (addIDigit === addJDigit) {
25+
max = Math.max((iItem + jItem), max);
26+
pair.push(iItem, jItem);
27+
}
28+
// Keep max
29+
j++;
30+
}
31+
i++;
32+
}
33+
34+
if (pair.length > 0) {
35+
return pair;
36+
}
37+
38+
return -1;
39+
}
40+
41+
module.exports = MaxPairNumberSum;

list/max-pair-sum/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 23/01/2020
4+
*/
5+
const maxPairNumberSum = require('./index');
6+
test('maxPairNumberSum is a function', () => {
7+
expect(typeof maxPairNumberSum).toEqual('function');
8+
});
9+
test('maxPairNumberSum [51, 71, 17, 42] to give [51, 42]', () => {
10+
expect(maxPairNumberSum([51, 71, 17, 42])).toEqual([51, 42]);
11+
});
12+
test('maxPairNumberSum [42, 33, 60] to give [42, 60]', () => {
13+
expect(maxPairNumberSum([42, 33, 60])).toEqual([42, 60]);
14+
});
15+
test('maxPairNumberSum [51, 32, 43] to give -1', () => {
16+
expect(maxPairNumberSum([51, 32, 43])).toEqual(-1);
17+
});

0 commit comments

Comments
 (0)