Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Expert
  • Last Updated : 12 Nov, 2021

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 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[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 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

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 cost
function 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 code
var arr = [19, 3, 6 ];
var n = arr.length;
var circumference = 20;
document.write( minCost(arr, n, circumference));
 
</script>
Output: 
7

 

Time Complexity: O(n * log n)

Auxiliary Space: O(n)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!