in

# Minimize K to let Person A consume at least ceil(N/(M + 1)) candies based on given rules

` `

`#include `

`using` `namespace` `std;`

` `

`bool` `check(``int` `K, ``int` `n, ``int` `m,`

`           ``vector<``int``> arr,`

`           ``int` `good_share)`

`    ``int` `candies = n, taken = 0;`

` `

`    ``while` `(candies > 0) `

` `

`        `

`        `

`        `

`        ``taken += min(K, candies);`

`        ``candies -= min(K, candies);`

` `

`        `

`        ``for` `(``int` `j = 0; j < m; j++) `

` `

`            `

`              `

`            ``int` `consume = (arr[j] * candies) / 100;`

` `

`            `

`              `

`            ``candies -= consume;`

`        `

`    `

` `

`    `

`      `

`    ``return` `(taken >= good_share);`

` `

`void` `minimumK(vector<``int``> &arr, ``int` `N,`

`              ``int` `M)`

`    `

`      `

`    ``int` `good_share = ``ceil``((N * 1.0) `

`                          ``/ ((M + 1) * 1.0));`

` `

`    ``int` `lo = 1, hi = N;`

` `

`      `

`      `

`    ``while` `(lo < hi) `

` `

`        `

`        ``int` `mid = (lo + hi) / 2;`

` `

`        `

`          `

`          `

`        ``if` `(check(mid, N, M, arr,`

`                  ``good_share)) `

` `

`            `

`            ``hi = mid;`

`        `

`       `

`          `

`          `

`        ``else`

`            ``lo = mid + 1;`

`        `

`    `

` `

`    `

`      `

`    ``cout << hi;`

` `

`int` `main()`

`    ``int` `N = 13, M = 1;`

`    ``vector<``int``> arr = 50 ;`

`   `

`    ``minimumK(arr, N, M);`

`   `

`      ``return` `0;`