• + 0 comments

    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.

    def bonetrousle(n, k, b):
        mini=maxi=0
        res=[0]*b
        temp=1
        while temp<=b:
            mini+=temp
            maxi+=k-temp+1
            temp+=1
        if n>=mini and n<=maxi:
            q=(n-mini)//b
            r=(n-mini)%b
            for i in range(b):
                res[i]+=(q+i+1)
            for i in range(1,r+1):
                res[-i]+=1
            return res
        else:
            return [-1]