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
|
160 Discussions
|
Please Login in order to post a comment
Hi. I am questioning the test cases. E.g. # 13, 1st input: 999999995000050000 10000000000000 100000 the output should be -1, but the expected output in the test is 9999999900001 9999999900002 9999999900003 9999999900004.
I do not understand why.
P.S: If I test my code with the custom input 999999995000050000 10000000000000 100000, I am receiveing the correct answer -1, the test passes.
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 formax
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