Minimize the cost of an array where
cost = summation of square of difference of two consecutive array elements.
ex:
arr = [1,2,3,4]
sum = (1-2)^2 + (2-3)^2 + (3-4)^2
Output = 3
if I insert any element in the range of (1 to 10^5) between any two items of the array then it will give some other value, like this I have to give the minimum cost possible by inserting a number.
entered_list = input("Enter a list, separating numbers with spaces: ").split()
test_list = list(map(int, entered_list))
cost = [((a - b) ** 2) for a, b in zip(test_list, test_list[1:])]
print("Cost: ", sum(cost))
Comments
Leave a comment