dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.4 KiB
1.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 691f7773cddba1caf1bf5eca | Challenge 131: Pairwise | 28 | challenge-131 |
--description--
Given an array of integers and a target number, find all pairs of elements in the array whose values add up to the target and return the sum of their indices.
For example, given [2, 3, 4, 6, 8] and 10, you will find two valid pairs:
2and8(2 + 8 = 10), whose indices are0and44and6(4 + 6 = 10), whose indices are2and3
Add all the indices together to get a return value of 9.
--hints--
pairwise([2, 3, 4, 6, 8], 10) should return 9.
assert.equal(pairwise([2, 3, 4, 6, 8], 10), 9);
pairwise([4, 1, 5, 2, 6, 3], 7) should return 15.
assert.equal(pairwise([4, 1, 5, 2, 6, 3], 7), 15);
pairwise([-30, -15, 5, 10, 15, -5, 20, -40], -20) should return 22.
assert.equal(pairwise([-30, -15, 5, 10, 15, -5, 20, -40], -20), 22);
pairwise([7, 9, 13, 19, 21, 6, 3, 1, 4, 8, 12, 22], 24) should return 10.
assert.equal(pairwise([7, 9, 13, 19, 21, 6, 3, 1, 4, 8, 12, 22], 24), 10);
--seed--
--seed-contents--
function pairwise(arr, target) {
return arr;
}
--solutions--
function pairwise(arr, target) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === target) {
total += i + j;
}
}
}
return total;
}