pairwise
Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices. If multiple pairs are possible, reuse indices wisely and aim for the smallest sum.
Examples
pairwise([1, 4, 2, 3, 0, 5], 7) → 11
pairwise([1, 3, 2, 4], 4) → 1
pairwise([1, 1, 1], 2) → 1
Starter code
function pairwise(arr, arg) {
}
Complexity of the optimal solution
Time O(n²), space O(n). This solution uses nested loops to check every possible pair of elements in the array, leading to a time complexity of O(n²). The `used` array can grow up to size n in the worst case, resulting in O(n) space complexity.