Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | Java implementation to find the 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
import java . util . * ; import java . util . Arrays ; class GFG { static int minCost ( int arr [ ] , int n , int circumference ) { Arrays . sort ( arr ) ; int [ ] arr2 = new int [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) res = Math . min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 19 , 3 , 6 } ; int n = arr . length ; int circumference = 20 ; System . out . println ( minCost ( arr , n , circumference ) ) ; } }