Given a circular ring which has marking from 1 to N. Given two numbers A and B, you can stand at any place(say X) and count the total sum of the distance(say Z i.e., distance from X to A + distance from X to B). The task is to choose X in such a way that Z is minimized. Print the value of Z thus obtained. Note that X cannot neither be equal to A nor be equal to B.
Input: N = 6, A = 2, B = 4
Choose X as 3, so that distance from X to A is 1, and distance from X to B is 1.
Input: N = 4, A = 1, B = 2
Choose X as 3 or 4, both of them gives distance as 3.
Approach: There are two paths between the positions A and B on the circle, one in clockwise direction and another in anti-clockwise. An optimal value for Z is to choose X as any point on the minimum path between A and B then Z will be equal to the minimum distance between the positions except for the case when both the positions are adjacent to each other i.e. the minimum distance is 1. In that case, X cannot be chosen as the point between them as it must be different from both A and B and the result will be 3.
Below is the implementation of the above approach:
- Minimum distance from a point to the line segment using Vectors
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Steps required to visit M points in order on a circular ring of N points
- Check if the given graph represents a Ring Topology
- Maximize minimum distance between repetitions from any permutation of the given Array
- Minimum distance between any special pair in the given array
- Minimum decrements to make integer A divisible by integer B
- Shortest distance between a Line and a Point in a 3-D plane
- Perpendicular distance between a point and a Line in 2 D
- Distance between a point and a Plane in 3 D
- Shortest distance between a point and a circle
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Find maximum distance between any city and station
- Find the integer points (x, y) with Manhattan distance atleast N
- Distance of chord from center when distance between center and another equal length chord is given
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
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.