Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

bigintegers's blog

By bigintegers, history, 5 years ago, In English

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.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.