Computer Science, asked by devaprasad4926, 1 year ago

Minimum cost to equal all elements of array using two operation geek for geeks practice

Answers

Answered by MzAbstruse
2

Explanation:

Given an array arr[] of n positive integers. There are two operations allowed:

Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a.

Operation 2 : Pick any index and increase its value by 1. It will cost b.

The task is to find the minimum cost to make all the elements equal in the array.

Examples:

Input : n = 4, a = 2, b = 3

arr[] = { 3, 4, 2, 2 }

Output : 5

Perform operation 2 on 3rd index

(0 based indexing). It will cost 2.

Perform operation 1 on index 1 (decrease)

and index 2 (increase). It will cost 3.

Input : n = 3, a = 2, b = 1

arr[] = { 5, 5, 5 }

Output : 0.

Similar questions