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

022 Candies

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

 /* 
n children have got m pieces of candy. They want to eat as much candy as they can, but each child must eat exactly the same amount of candy as any other child. Determine how many pieces of candy will be eaten by all the children together. Individual pieces of candy cannot be split.

Example

For n = 3 and m = 10, the output should be candies(n, m) = 9.

Each child will eat 3 pieces. So the answer is 9. 
*/

function candies(n,m) {
  const candy = Math.floor(m/n)
  return n * candy
}

test('candies', () => {
  expect(candies(3,10)).toEqual(9)
});

Created 2019-12-09T03:27:46+05:30 · Edit