Given a list of N integers, what's the minimum number of operations you need to perform to make them all equal? An "operation" consists of subtracting one number by one, and adding another number by two. Or, when is it not possible?
I think this was a problem in a codeforces or topcoder, but I cannot remember where. For example, if N = 5 and we have Array = [6, 1, 1, 1, 1], you can make them all equal to 2 in just four operations.
Clearly, they all need to become equal to Sum/N, but the hard part is determining whether it is possible or not. I was trying to do this with a while loop but it is getting really slow when I try for large numbers.
Sorry if this is a really silly question but I would appreciate any assistance with this problem. This seems like a really simple problem, but I am struggling. Additionally, if anyone recognizes this problem please link me in the comments so I can try to submit.
An operation consists of subtracting one number by one, and adding another number by two
Did you mean add 1 and subtract 1? The example [6,1,1,1,1] does not work for above operation.