Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the final result ; Driver code
function minCost ( arr , n , circumference ) { arr . sort ( ( a , b ) => a - b ) var arr2 = Array ( 2 * n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } var res = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) res = Math . min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } var arr = [ 19 , 3 , 6 ] ; var n = arr . length ; var circumference = 20 ; document . write ( minCost ( arr , n , circumference ) ) ;