Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function pairwise (values: number[], check: number): number {
- const sorted = Array.from(values.entries());
- sorted.sort((a, b) => {
- let res = Math.sign(a[1] - b[1]);
- if (res === 0) {
- res = Math.sign(a[0] - b[0]);
- }
- return res;
- });
- let i = 0;
- let j = sorted.length - 1;
- let total = 0;
- while (i < j) {
- const sum = sorted[i][1] + sorted[j][1];
- if (sum < check) {
- i++;
- continue;
- }
- if (sum > check) {
- j--;
- continue;
- }
- let k = j - 1;
- while (k > i && sorted[k][1] === sorted[j][1]) {
- k--;
- }
- k++;
- total += sorted[i][0] + sorted[k][0];
- i++;
- j = k - 1;
- }
- return total;
- }
Add Comment
Please, Sign In to add comment