code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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