We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Bonetrousle

# Bonetrousle

#### Sort by

recency

#### |

#### 159 Discussions

#### |

Please Login in order to post a comment

python3: pure math solution

Single-pass (O(b)) without constructing an array

## Overview

First calulate the min and max achievable with given k and b (ie. what's the min and max given how many boxes we will use and how many boxes the store carries). Check if we are able to achieve the sum at all, otherwise return -1.

Next up calculate how many sticks we can add to each box (our quotient) and how many sticks we will need to distribute after that (our remainder). Then we simply shift our number series according to our quotient, and distribute the remainder.

Example: buy 10 strains using 3 boxes, store carries 5 boxes. Start out with the series 1 + 2 + 3, sum is 6. We calculate our quotient 1 and remainder 1. This means we can shift our series by one and get 2 + 3 + 4, sum is 9. Find a way to distribute our 1 remainder: 2 + 3 + 5 = 10. Done.

## C++ quirks and overflows

Had some issues with overflow even after plastering

`uint64_t`

all over the place, turns out my formula for`max`

was laid out in such a way that one intermediate result produced an overflow, this was solved by shamelessy stealing the formula from another solution on here and having ChatGPT explain why that one works compared to my initial formula...To avoid creating an array I just pass the out stream to the function and write directly to that.

Determine the highest and lowest possible sums using the provided value 'k'. Compare these sums with 'b' to ascertain if the sum 'n' is achievable using the given 'b' value. Next, calculate the required value by subtracting the minimum value from 'n', and then distribute it equally among all the values in the result array. Afterwards, distribute the remainder equally in the result array from the last position to prevent any overlap. Finally, this result array is the definitive solution.

Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Bonetrousle Problem Solution

Here is the solution of Bonetrousle Click Here