if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance
b [ x ] = Math . Min ( d , b [ x ] ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ;