The maximum number of equal elements in an array after n conversion

You have an array containing n elements. With any movement, you select two indices, i and j, I do not equal j and the increment value at one index and decrease the value at the other index. You can make this movement as many times as you like. We need the maximum number of elements that can have the same value (after any number of moves). The example answer 1,2,3,4 is 3, since we can have no more than 3 elements that are equal after applying the displacements any number of times. But I'm looking for an algorithm to make the help so needed.

+4
source share
3 answers

, . , , N N-1 ( N - ).

. , : sum(1,2,3,4) = 10.

sum % N == 0, N. sum/N . , .

N-1. N-1 , (int)sum/N, . "", / , .

, O (N). N, , . " ", , .

+3

, - .

def elems(array, previous_attempts = []):
    # Get a list of all the possible arrays (after transforming the current array)
    # and remove all of the arrays we have seen so far.
    possible_arrays = possibilities(array) - previous_attempts

    # If there are no more possible, return the number of elements the array
    # has in common.
    if possible_arrays is empty:
        return num_in_common(array)

    # Otherwise, for all the possibilities find the one that creates the maximum
    # amount of elements in common.
    max = 0
    for a in possible_arrays:
        # This is a recursive call that calculates the score for the possibility.
        # It also keeps track of the current state so we don't revisit it.
        score = elems(a, previous_attempts.append(array))
        if score > max:
            max = score
    return max
0

( A N). A max ( ), , max , max+1 .

:

  • if max=N N
  • if max=N-1 N-1

V, max , , V, V. , max+2 ( , V). i j, max+1 ( , V).

EDIT: , i j , / , (, ).

0

All Articles