CHECKING SUM ZERO - PROBLEM JAVASCRIPT

PHOTO EMBED

Tue Dec 20 2022 08:11:19 GMT+0000 (Coordinated Universal Time)

Saved by @YaseenBhojani #javascript

const sumZeroPair = (arr) => {
  let left = 0, // -4
    right = arr.length - 1; // 4

  while (left < right) {
    let sum = arr[left] + arr[right]; // 0
    if (sum === 0) return [arr[left], arr[right]]; // true
    else if (sum > 0) right--;
    else left++;
  }
};

const res = sumZeroPair([-5, -4, -3, -2, 0, 2, 4, 6, 8]);
console.log(res);
content_copyCOPY