# Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road

Given circumference of the circle and an array pos[] which marks the distance of N points on circle relative to a fixed point in the clockwise direction. We have to find a minimum distance through which we can visit all points. We can start with any point.

Examples:

Input: circumference = 20, pos = [3, 6, 9]
Output: min path cost =6
Explanation:
If we start from 3, we go to 6 and then we go to 9. Threfore, total path cost is 3 units for first movement and 3 units for second movement which sums up to 6.

Input:circumference=20 pos = [3, 6, 19]
Output: min path cost = 7
Explanation :
If we start from 19 and we go to 3 it will cost 4 units because we go from 19 -> 20 -> 1 -> 2 -> 3 which gives 4 units, and then 3 to 6 which gives 3 units. In total minimum cost will be 4 + 3 = 7.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :

To solve the problem mentioned above we have to follow the steps given below:

• Sort the array which marks the distance of N points on circle.
• Make the array size twice by adding N element with value arr[i + n] = circumference + arr[i].
• Find the minimum value (arr[i + (n-1)] – arr[i]) for all valid iterations of value i.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the  ` `// Minimum Cost Path to visit all nodes ` `// situated at the Circumference of  ` `// Circular Road ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the minimum cost ` `int` `minCost(``int` `arr[], ``int` `n, ``int` `circumference) ` `{ ` `    ``// Sort the given array ` `    ``sort(arr, arr + n); ` ` `  `    ``// Initialise a new array of double size ` `    ``int` `arr2[2 * n]; ` ` `  `    ``// Fill the array elements ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``arr2[i] = arr[i]; ` `        ``arr2[i + n] = arr[i] + circumference; ` `    ``} ` ` `  `    ``// Find the minimum path cost ` `    ``int` `res = INT_MAX; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``res = min(res, arr2[i + (n - 1)] - arr2[i]); ` ` `  `    ``// Return the final result ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 19, 3, 6 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``int` `circumference = 20; ` ` `  `    ``cout << minCost(arr, n, circumference); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the  ` `// Minimum Cost Path to visit all nodes ` `// situated at the Circumference of  ` `// Circular Road ` `import` `java.util.*;  ` `import` `java. util. Arrays; ` ` `  `class` `GFG {  ` ` `  `// Function to find the minimum cost ` `static` `int` `minCost(``int` `arr[], ``int` `n, ` `                   ``int` `circumference) ` `{ ` `    ``// Sort the given array ` `    ``Arrays.sort(arr); ` ` `  `    ``// Initialise a new array of double size ` `    ``int``[] arr2 = ``new` `int``[``2` `* n]; ` ` `  `    ``// Fill the array elements ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``arr2[i] = arr[i]; ` `       ``arr2[i + n] = arr[i] + circumference; ` `    ``} ` ` `  `    ``// Find the minimum path cost ` `    ``int` `res = Integer.MAX_VALUE; ` ` `  `    ``for``(``int` `i = ``0``; i < n; i++) ` `       ``res = Math.min(res,  ` `                      ``arr2[i + (n - ``1``)] - ` `                      ``arr2[i]); ` ` `  `    ``// Return the final result ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `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)); ` `} ` ` `  `} ` ` `  `// This code is contributed by ANKITKUMAR34 `

## Python3

 `# Python3 implementation to find the  ` `# minimum cost path to visit all nodes ` `# situated at the circumference of  ` `# circular Road ` ` `  `# Function to find the minimum cost ` `def` `minCost(arr, n, circumference): ` ` `  `    ``# Sort the given array ` `    ``arr.sort() ` ` `  `    ``#Initialise a new array of double size ` `    ``arr2 ``=` `[``0``] ``*` `(``2` `*` `n) ` ` `  `    ``# Fill the array elements ` `    ``for` `i ``in` `range``(n): ` `        ``arr2[i] ``=` `arr[i] ` `        ``arr2[i ``+` `n] ``=` `arr[i] ``+` `circumference ` ` `  `    ``# Find the minimum path cost ` `    ``res ``=` `9999999999999999999``; ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``res ``=` `min``(res,  ` `                  ``arr2[i ``+` `(n ``-` `1``)] ``-` `                  ``arr2[i]); ` ` `  `    ``# Return the final result ` `    ``return` `res; ` ` `  `# Driver code ` `arr ``=` `[ ``19``, ``3``, ``6` `]; ` `n ``=` `len``(arr) ` `circumference ``=` `20``; ` ` `  `print``(minCost(arr, n, circumference)) ` ` `  `# This code is contributed by ANKITKUMAR34 `

## C#

 `// C# implementation to find the  ` `// Minimum Cost Path to visit all nodes ` `// situated at the Circumference of  ` `// Circular Road ` `using` `System; ` ` `  `class` `GFG{  ` ` `  `// Function to find the minimum cost ` `static` `int` `minCost(``int` `[]arr, ``int` `n, ` `                   ``int` `circumference) ` `{ ` `    ``// Sort the given array ` `    ``Array.Sort(arr); ` ` `  `    ``// Initialise a new array of double size ` `    ``int``[] arr2 = ``new` `int``[2 * n]; ` ` `  `    ``// Fill the array elements ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `       ``arr2[i] = arr[i]; ` `       ``arr2[i + n] = arr[i] + circumference; ` `    ``} ` ` `  `    ``// Find the minimum path cost ` `    ``int` `res = ``int``.MaxValue; ` ` `  `    ``for``(``int` `i = 0; i < n; i++) ` `       ``res = Math.Min(res, arr2[i + (n - 1)] - ` `                           ``arr2[i]); ` ` `  `    ``// Return the readonly result ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = { 19, 3, 6 }; ` `    ``int` `n = arr.Length; ` `    ``int` `circumference = 20; ` ` `  `    ``Console.WriteLine(minCost(arr, n, circumference)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```7
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ANKITKUMAR34, princi singh

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.