• + 1 comment

    Time complexity can be reduced for the average case from O(n^2) to O(n) by recording the last index of all encountered numbers in a dictionary (worst case remains at O(n^2)):

    #!/bin/python3
    
    
    _ = input()
    
    minimum = -1
    i = {}
    for j, a in enumerate(int(s) for s in input().split()):
        if a in i:
            d = j - i[a]
            if minimum < 0 or d < minimum:
                minimum = d
        i[a] = j
    
    print(minimum)