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++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def minCost(arr, n, circumference):
arr.sort()
arr2 = [ 0 ] * ( 2 * n)
for i in range (n):
arr2[i] = arr[i]
arr2[i + n] = arr[i] + circumference
res = 9999999999999999999 ;
for i in range (n):
res = min (res,
arr2[i + (n - 1 )] -
arr2[i]);
return res;
arr = [ 19 , 3 , 6 ];
n = len (arr)
circumference = 20 ;
print (minCost(arr, n, circumference))
|
C#
using System;
class GFG{
static int minCost( int []arr, int n,
int circumference)
{
Array.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 = int .MaxValue;
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;
Console.WriteLine(minCost(arr, n, circumference));
}
}
|
Javascript
<script>
function minCost(arr, n,circumference)
{
arr.sort((a,b)=>a-b)
var arr2 = Array(2* n).fill(0);
for ( var i = 0; i < n; i++) {
arr2[i] = arr[i];
arr2[i + n] = arr[i] + circumference;
}
var res = 1000000000;
for ( var i = 0; i < n; i++)
res = Math.min(res, arr2[i + (n - 1)] - arr2[i]);
return res;
}
var arr = [19, 3, 6 ];
var n = arr.length;
var circumference = 20;
document.write( minCost(arr, n, circumference));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(n)