Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created December 7, 2022 10:54
Show Gist options
  • Save Shaddyjr/6ab463d2330af01f8feca28cec33bb54 to your computer and use it in GitHub Desktop.
Save Shaddyjr/6ab463d2330af01f8feca28cec33bb54 to your computer and use it in GitHub Desktop.
// source: https://www.hackerrank.com/challenges/chocolate-feast/problem
// video: https://youtu.be/TPjNYmUzOdg
function chocolateFeast(n, c, m) {
// initial buy
let bars = Math.floor(n/c)
let wrappers = bars
while (wrappers >= m){
// buy more bars
const additionalBars = Math.floor(wrappers/m)
bars += additionalBars
// handle reduction of old & addition of new wrappers
wrappers = wrappers % m
wrappers += additionalBars
}
return bars
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment