Comments

Karan Kumar a year ago

Time complexity O(N):

const solve = (intArray, pair) => {
  let leftMap  = {};
  let rightMap = {};
  
  for (let i=0; i<intArray.length; i++) {
    let diffX = pair[0] - intArray[i][0];
    let diffY = pair[1] - intArray[i][1];
    if ((diffX in leftMap) && (diffY in rightMap)) return true 
    else {
      leftMap[intArray[i][0]] = true; 
      rightMap[intArray[i][1]] = true;
    }
  }
  return false;
}; 

Mauritius Rupee Oklahoma3 years ago

const solve = (intArray, pair) => {
  const pairSum = pair[0] + pair[1];
  const sumArray = intArray.map((arr) => {
    return arr[0] + arr[1];
  })
  for (let int of sumArray) {
    for (let i = 0; i < sumArray.length; i++) {
      if (int + sumArray[i] === pairSum) return true;
    }
  }
  return false;
};

Abdelhamid Ismail3 years ago

const solve = (intArray, pair) => {
  return intArray.some(innerArr => {
    return intArray.find(a => a[0] === (pair[0]-innerArr[0]) && a[1] === (pair[1]-innerArr[1])  )
  } )
};