Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | Function to find distribution of M objects among all array elements ; Stores the distribution of M objects ; Stores the indices of distribution ; Stores the remaining objects ; Iterate until rem is positive ; If the number of remaining objects exceeds required the number of objects ; Increase the number of objects for the index ptr by arr [ ptr ] ; Decrease remaining objects by arr [ ptr ] ; Increase the number of objects for the index ptr by rem ; Decrease remaining objects to 0 ; Increase ptr by 1 ; Print the final distribution ; Driver Code
def distribute ( N , K , M , arr ) : NEW_LINE INDENT distribution = [ 0 ] * N NEW_LINE ptr = K - 1 NEW_LINE rem = M NEW_LINE while ( rem > 0 ) : NEW_LINE INDENT if ( rem >= arr [ ptr ] ) : NEW_LINE INDENT distribution [ ptr ] += arr [ ptr ] NEW_LINE rem -= arr [ ptr ] NEW_LINE DEDENT else : NEW_LINE INDENT distribution [ ptr ] += rem NEW_LINE rem = 0 NEW_LINE DEDENT ptr = ( ptr + 1 ) % N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( distribution [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 2 , 1 , 4 ] NEW_LINE M = 11 NEW_LINE S = 2 NEW_LINE N = len ( arr ) NEW_LINE distribute ( N , S , M , arr ) NEW_LINE