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

441 Arranging Coins

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

const arrangingCoins = n => {
  let left = 0
  let right = n
  let result = 0
  while (left <= right) {
    const mid = Math.trunc((left + right) / 2)
    const coinsNeeded = (mid / 2) * (mid + 1)
    if (coinsNeeded > n) {
      right = mid - 1
    } else {
      left = mid + 1
      result = Math.max(result, mid)
    }
  }
  return result
}

console.log(arrangingCoins(5))

Created 2022-03-21T15:49:44+00:00 · Edit