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:
CPP
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; // Function to return the minimum distance int min_distance(int n, vector<int> arr) { // Declare a Pair[] array of size n vector<pair<int,int>> val(n); // Store all the (element, index) pairs for ( int i = 0; i < n; i++) val[i] = {arr[i], i}; // Sort the pairs in ascending order of the value sort(val.begin(), val.end()); int min_dist = 0; for (int i = 1; i < n; i++) { // Choose the minimum distance path // of the two available min_dist += min(abs(val[i].second - val[i - 1].second), n - abs(val[i].second - val[i - 1].second)); } return min_dist; } // Driver Code int main() { vector<int> arr = {3, 6, 5, 1, 2, 4}; int n = arr.size(); cout << (min_distance(n, arr)); } // This code is contributed by mohit kumar 29 |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the minimum distance static int min_distance(int n, int[] arr) { // Declare a Pair[] array of size n Pair[] val = new Pair[n]; // Store all the (element, index) pairs for (int i = 0; i < n; i++) val[i] = new Pair(arr[i], i); // Sort the pairs in ascending order of the value Arrays.sort(val, com); int min_dist = 0; for (int i = 1; i < n; i++) { // Choose the minimum distance path // of the two available 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; } // Comparator to be used in the // sorting of pairs based on the values 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); } }; // Class to represent a pair static class Pair { int x, y; Pair(int p, int q) { x = p; y = q; } } // Driver code 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
# Python implementation of the approach # Function to return the minimum distance def min_distance(n, arr): # Declare a Pair[] array of size n val = [None] * n # Store all the (element, index) pairs for i in range(0, n): val[i] = (arr[i], i) # Sort the pairs in ascending order of the value val.sort() min_dist = 0 for i in range(1, n): # Choose the minimum distance path # of the two available min_dist += min(abs(val[i][1] - val[i - 1][1]), n - abs(val[i][1] - val[i - 1][1])) return min_dist # Driver code if __name__ == "__main__": arr = [3, 6, 5, 1, 2, 4] n = len(arr) print(min_distance(n, arr)) # This code is contributed by Rituraj Jain |
8
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.
Recommended Posts:
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Rearrange all elements of array which are multiples of x in increasing order
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Print array elements in alternatively increasing and decreasing order
- Sort array such that absolute difference of adjacent elements is in increasing order
- C program to traverse an Array
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Length of longest increasing circular subarray
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Sort even and odd placed elements in increasing order
- Maximum sum of increasing order elements from n arrays
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Sort a Matrix in all way increasing order
- Print all numbers in given range having digits in strictly increasing order
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Dijkstra's shortest path with minimum edges
- Shortest path in a directed graph by Dijkstra’s algorithm
- Shortest Path with even number of Edges from Source to Destination
- Create a Graph by connecting divisors from N to M and find shortest path
- Maximize shortest path between given vertices by adding a single edge
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.

