Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int n , int circumference ) { sort ( arr , arr + n ) ; int arr2 [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) res = min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } int main ( ) { int arr [ ] = { 19 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int circumference = 20 ; cout << minCost ( arr , n , circumference ) ; return 0 ; }