Shortest path to traverse all the elements of a circular array in increasing order
Last Updated :
24 Jan, 2023
There are N distinct integers arranged on a circle. The distance between any two adjacent numbers is 1. The task is to travel on this circle starting with the smallest number, then moving to the second smallest, third smallest, and so on until the largest number and print the minimum travel distance.
Examples:
Input: arr[] = {3, 6, 5, 1, 2, 4}
Output: 8
1 -> 2 (Left to right)
2 -> 3 (Left to right)
3 -> 4 (Right to left)
4 -> 5 (Left to right or right to left)
5 -> 6 (Right to left)
Input: arr[] = {14, 16, 8, 17, 12, 10, 4, 13, 11, 20}
Output: 27
Approach: If we want to travel between two elements with indices i and j, we have two possible paths, one of length |i – j| and the other of length n – |i – j| and we will take the one with the minimum distance.
Initially, we build an array of pairs of (element, index) and we sort it in increasing order of the elements. Then we can take every two consecutive pairs and find the shortest way to travel between these two elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int min_distance( int n, vector< int > arr)
{
vector<pair< int , int >> val(n);
for ( int i = 0; i < n; i++)
val[i] = {arr[i], i};
sort(val.begin(), val.end());
int min_dist = 0;
for ( int i = 1; i < n; i++)
{
min_dist += min( abs (val[i].second - val[i - 1].second),
n - abs (val[i].second - val[i - 1].second));
}
return min_dist;
}
int main()
{
vector< int > arr = {3, 6, 5, 1, 2, 4};
int n = arr.size();
cout << (min_distance(n, arr));
}
|
Java
import java.util.*;
class GFG {
static int min_distance( int n, int [] arr)
{
Pair[] val = new Pair[n];
for ( int i = 0 ; i < n; i++)
val[i] = new Pair(arr[i], i);
Arrays.sort(val, com);
int min_dist = 0 ;
for ( int i = 1 ; i < n; i++) {
min_dist
+= Math.min(Math.abs(val[i].y - val[i - 1 ].y),
n - Math.abs(val[i].y - val[i - 1 ].y));
}
return min_dist;
}
static final Comparator<Pair> com = new Comparator<Pair>() {
public int compare(Pair a, Pair b)
{
if (Integer.compare(a.x, b.x) != 0 )
return Integer.compare(a.x, b.x);
else
return Integer.compare(a.y, b.y);
}
};
static class Pair {
int x, y;
Pair( int p, int q)
{
x = p;
y = q;
}
}
public static void main(String[] args)
{
int [] arr = new int [] { 3 , 6 , 5 , 1 , 2 , 4 };
int n = arr.length;
System.out.println(min_distance(n, arr));
}
}
|
Python
def min_distance(n, arr):
val = [ None ] * n
for i in range ( 0 , n):
val[i] = (arr[i], i)
val.sort()
min_dist = 0
for i in range ( 1 , n):
min_dist + = min ( abs (val[i][ 1 ] - val[i - 1 ][ 1 ]),
n - abs (val[i][ 1 ] - val[i - 1 ][ 1 ]))
return min_dist
if __name__ = = "__main__" :
arr = [ 3 , 6 , 5 , 1 , 2 , 4 ]
n = len (arr)
print (min_distance(n, arr))
|
Javascript
<script>
function min_distance(n,arr)
{
let val = new Array(n);
for (let i = 0; i < n; i++)
val[i] = [arr[i], i];
val.sort( function (a,b){ return a[0]-b[0];});
let min_dist = 0;
for (let i = 1; i < n; i++) {
min_dist
+= Math.min(Math.abs(val[i][1] - val[i - 1][1]),
n - Math.abs(val[i][1] - val[i - 1][1]));
}
return min_dist;
}
let arr=[3, 6, 5, 1, 2, 4];
let n=arr.length;
document.write(min_distance(n, arr))
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int min_distance( int n, int [] arr)
{
Pair[] val = new Pair[n];
for ( int i = 0; i < n; i++)
val[i] = new Pair(arr[i], i);
Array.Sort(val, com);
int min_dist = 0;
for ( int i = 1; i < n; i++)
{
min_dist
+= Math.Min(Math.Abs(val[i].y - val[i - 1].y),
n - Math.Abs(val[i].y - val[i - 1].y));
}
return min_dist;
}
static readonly IComparer<Pair> com = new PairComparer();
class PairComparer : IComparer<Pair>
{
public int Compare(Pair a, Pair b)
{
if (Comparer< int >.Default.Compare(a.x, b.x) != 0)
return Comparer< int >.Default.Compare(a.x, b.x);
else
return Comparer< int >.Default.Compare(a.y, b.y);
}
}
class Pair
{
public int x, y;
public Pair( int p, int q)
{
x = p;
y = q;
}
}
public static void Main( string [] args)
{
int [] arr = new int [] { 3, 6, 5, 1, 2, 4 };
int n = arr.Length;
Console.WriteLine(min_distance(n, arr));
}
}
|
Complexity Analysis:
- Time Complexity: O(n * log(n))
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...