Answer to Question #235941 in Python for Shravani

Question #235941
Minimum cost of adding all the numbers in the array
1
Expert's answer
2021-09-11T07:19:55-0400
def minimumCost(arr, N, M):
     
    # Sorting the given array in
    # increasing order
    arr.sort()
     
    # To store the prefix sum of arr[]
    pref = []
     
    pref.append(arr[0])
     
    for i in range(1, N):
        pref.append(arr[i] + pref[i - 1])
     
    # Update the pref[] to find the cost
    # selecting array element by selecting
    # at most M element
    for i in range(M, N):
        pref[i] += pref[i - M]
     
    # Print the pref[] for the result
    for i in range(N):
        print(pref[i], end = ' ')
 
# Driver Code
arr = [ 6, 19, 3, 4, 4, 2, 6, 7, 8 ]
M = 2
N = len(arr)
 
minimumCost(arr, N, M);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS