Increment of decrement array values to get pairwise distinct elements

Viewed 544

There is an array A of N integers in the range [1, ..., N].

I one move, you can either increase or decrease the value of any element by 1. After each move, all elements should remain in the range [1, ..., N]

find the smallest number of moves to make all the elements in the array pairwaise distinct, as in no value can appear more than once.

Here is my code for this:

def solution(A, changes = 0):
    A.sort()
    for i in range(len(A)):
        changes += abs(A[i] - (i + 1))
    return changes
solution([6, 2, 3, 5, 6, 3]) -> returns 4
solution([1, 2, 1]) -> returns 2
solution([2, 1, 4, 4]) -> returns 1

My question is, is there a way to do this in linear time? currently my solution take nlogn, because of the sort

1 Answers

Yes, counting sort makes it linear.

Related