gangachris
3/7/2017 - 10:55 AM

Given x handshakes, get the number of guests in a meeting, and assuming each guest made a handshake to each other guest once. Get the number

Given x handshakes, get the number of guests in a meeting, and assuming each guest made a handshake to each other guest once. Get the number of guests. Common aptitude problem

// After studying the problem, you'll see an arithemetic progression like pattern
// Using the sum of arithmetic progression could easily work too.
// But on further inspection, it's a simple consectuive numbers problem.
function getConsecutiveDevisors(num) {
  let lowerDevisor = Math.floor(Math.sqrt(num))
  let higherDevisor = lowerDevisor + 1;

  if (lowerDevisor * higherDevisor === num) {
    return [lowerDevisor, lowerDevisor + 1]
  }

  return false
}

// In short, multiply the number of handshakes by two, 
// Then find two concecutive numbers whose product gives the result,
// Then pick the higher number
function getGuests(num) {
  let devisors = getConsecutiveDevisors(num * 2);
  if (devisors) {
    return devisors[1];
  }

  return 'Mathematically Impossible'
}

console.log(getGuests(45))