JSFiddle - React, Tailwind, and code Playground

by moeishaa

JavaScript

const pair = list => {
	let cache = Object.create(null),
  	result = -1,
  	max = Number.MIN_VALUE,
    i = 0,
    j = list.length - 1;
  
  list.forEach(n => {
  	max = Math.max(max, n);
    cache[n] = true;
  });

	list.sort();

  while (i < j) {
  	let tmp = list[i] * list[j];
    
    if (tmp in cache) {
    	result = tmp;
      break;
    }
    else if (tmp < max) {
    	i++;
    }
    else {
    	j--;
    }
  }
  
  return result;
};

let a = [10, 3, 5, 30, 35],
	b = [2, 5, 7, 8],
  c = [10, 2, 4, 30, 35],
  d = [10, 2, 2, 4, 30, 35];

console.log(pair(a));
console.log(pair(b));
console.log(pair(c));
console.log(pair(d));