code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

374 Guess Number Higher Or Lower

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const GuessNumberHigherOrLower = val => {
  let left = 0
  let right = val
  while (left <= val) {
    const mid = Math.trunc((left + right) / 2)
    const response = guess(mid)
    if (response > 0) {
      left = mid + 1
    } else if (response < 0) {
      right = mid - 1
    } else {
      return mid
    }
  }
  return -1
}

console.log(GuessNumberHigherOrLower(2))

Created 2022-03-21T14:33:35+00:00 · Edit