Guest User

Untitled

a guest
Feb 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. export function pairwise (values: number[], check: number): number {
  2. const sorted = Array.from(values.entries());
  3.  
  4. sorted.sort((a, b) => {
  5. let res = Math.sign(a[1] - b[1]);
  6. if (res === 0) {
  7. res = Math.sign(a[0] - b[0]);
  8. }
  9.  
  10. return res;
  11. });
  12.  
  13. let i = 0;
  14. let j = sorted.length - 1;
  15. let total = 0;
  16.  
  17. while (i < j) {
  18. const sum = sorted[i][1] + sorted[j][1];
  19. if (sum < check) {
  20. i++;
  21. continue;
  22. }
  23. if (sum > check) {
  24. j--;
  25. continue;
  26. }
  27. let k = j - 1;
  28. while (k > i && sorted[k][1] === sorted[j][1]) {
  29. k--;
  30. }
  31. k++;
  32. total += sorted[i][0] + sorted[k][0];
  33. i++;
  34. j = k - 1;
  35. }
  36.  
  37. return total;
  38. }
Add Comment
Please, Sign In to add comment