[Python] minimum moves needed for distinct array [closed]

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    You are given an array A containing N integers, where each integer is in the range [1..N]. In each move, you can increase or decrease the value of any element by 1, with the constraint that after each move, all numbers must remain in the range [1..N]. Your objective is to determine the minimum number of moves required to ensure that all elements in the array are distinct, meaning no number appears more than once in the array.

    You are tasked with implementing a function:

    int solution(int A[]);

    If the result exceeds 1,000,000,000, the function should return -1.

    solution([6, 2, 3, 5, 6, 3]) -> returns 4

    solution([1, 2, 1]) -> returns 2

    solution([2, 1, 4, 4]) -> returns 1

    Continue reading...

Compartilhe esta Página