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.
Input: circumference = 20, pos = [3, 6, 9]
Output: min path cost =6
If we start from 3, we go to 6 and then we go to 9. Threfore, 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
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.
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:
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.
- Steps required to visit M points in order on a circular ring of N points
- Number of Distinct Meeting Points on a Circular Road
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Program to find Perimeter / Circumference of Square and Rectangle
- Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Print path from root to all nodes in a Complete Binary Tree
- Min Cost Path | DP-6
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Minimum cost to make all array elements equal
- Minimum Cost to make all array elements equal using given operations
- Minimum cost required to convert all Subarrays of size K to a single element
- Maximize count of nodes disconnected from all other nodes in a Graph
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Minimum steps to come back to starting point in a circular tour
- Minimum rotations to unlock a circular lock
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.