Minimum Operations to Make Array Equal

Minimum Operations to Make Array Equal you are given array of length n. You can choose any two index & subtract / add one to that index element. Final goal is to make all elements equal & return minimum number in doing so.

 

Now coming to approach there can be two cases one for odd & other for even length. 

 

For odd length what you can do is to transfer some amount from bigger to smaller. For example : 1, 3, 5 in this you can subtract 2 from 5 & add it to 1. For 1, 3, 5, 7, 9 you can transfer 4 from 9 to 1, 2 from 7 & add it to 3. Then all will become 5, 5, 5, 5, 5. Basically we have to make all elements equal to middle element.


For even length we will find average of two number & make both the number equal to their average. 

For Example : In 1, 3, 5, 7, 9, 11 average of 5 & 7 is 6, average of 9 & 3 is 6, 11 & 1 is 6. We will subtract & add numbers accordingly to get the final result.


Solution

 

Similar Question : Link to Question & Explanation 

No comments:

If you have any doubt or suggestion let me know in comment section.

Powered by Blogger.