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. Therefore, 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.Â
Â
Â
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 <bits/stdc++.h>using namespace std;Â
// Function to find the minimum costint 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 codeint main(){Â Â Â Â int arr[] = { 19, 3, 6 };Â
    int n = sizeof(arr) / sizeof(arr[0]);Â
    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 Roadimport java.util.*; import java. util. Arrays;Â
class GFG { Â
// Function to find the minimum coststatic 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 codepublic 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 costdef 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 codearr = [ 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 Roadusing System;Â
class GFG{ Â
// Function to find the minimum coststatic 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 codepublic 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 |
Javascript
<script>Â
// Javascript implementation to find the // Minimum Cost Path to visit all nodes// situated at the Circumference of // Circular RoadÂ
// Function to find the minimum costfunction minCost(arr, n,circumference){    // Sort the given array    arr.sort((a,b)=>a-b)Â
    // Initialise a new array of double size    var arr2 = Array(2* n).fill(0);Â
    // Fill the array elements    for (var i = 0; i < n; i++) {        arr2[i] = arr[i];        arr2[i + n] = arr[i] + circumference;    }Â
    // Find the minimum path cost    var res = 1000000000;Â
    for (var i = 0; i < n; i++)        res = Math.min(res, arr2[i + (n - 1)] - arr2[i]);Â
    // Return the final result    return res;}Â
// Driver codevar arr = [19, 3, 6 ];var n = arr.length;var circumference = 20;document.write( minCost(arr, n, circumference));Â
</script> |
7
Â
Time Complexity: O(n * log n)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



