# Find the minimum sum of distance to A and B from any integer point in a ring of size N

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**.

**Examples:**

Input:N = 6, A = 2, B = 4

Output:2

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

Output:3

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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum value of Z ` `int` `findMinimumZ(` `int` `n, ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Change elements such that a < b ` ` ` `if` `(a > b) ` ` ` `swap(a, b); ` ` ` ` ` `// Distance from (a to b) ` ` ` `int` `distClock = b - a; ` ` ` ` ` `// Distance from (1 to a) + (b to n) ` ` ` `int` `distAntiClock = (a - 1) + (n - b + 1); ` ` ` ` ` `// Minimum distance between a and b ` ` ` `int` `minDist = min(distClock, distAntiClock); ` ` ` ` ` `// If both the positions are ` ` ` `// adjacent on the circle ` ` ` `if` `(minDist == 1) ` ` ` `return` `3; ` ` ` ` ` `// Return the minimum Z possible ` ` ` `return` `minDist; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4, a = 1, b = 2; ` ` ` `cout << findMinimumZ(n, a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum value of Z ` ` ` `static` `int` `findMinimumZ(` `int` `n, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `// Change elements such that a < b ` ` ` `if` `(a > b) ` ` ` `{ ` ` ` `swap(a, b); ` ` ` `} ` ` ` ` ` `// Distance from (a to b) ` ` ` `int` `distClock = b - a; ` ` ` ` ` `// Distance from (1 to a) + (b to n) ` ` ` `int` `distAntiClock = (a - ` `1` `) + (n - b + ` `1` `); ` ` ` ` ` `// Minimum distance between a and b ` ` ` `int` `minDist = Math.min(distClock, distAntiClock); ` ` ` ` ` `// If both the positions are ` ` ` `// adjacent on the circle ` ` ` `if` `(minDist == ` `1` `) ` ` ` `{ ` ` ` `return` `3` `; ` ` ` `} ` ` ` ` ` `// Return the minimum Z possible ` ` ` `return` `minDist; ` ` ` `} ` ` ` ` ` `private` `static` `void` `swap(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `, a = ` `1` `, b = ` `2` `; ` ` ` `System.out.println(findMinimumZ(n, a, b)); ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` `# Function to return the minimum value of Z ` `def` `findMinimumZ(n, a, b): ` ` ` ` ` `# Change elements such that a < b ` ` ` `if` `(a > b): ` ` ` `temp ` `=` `a ` ` ` `a ` `=` `b ` ` ` `b ` `=` `temp ` ` ` ` ` `# Distance from (a to b) ` ` ` `distClock ` `=` `b ` `-` `a ` ` ` ` ` `# Distance from (1 to a) + (b to n) ` ` ` `distAntiClock ` `=` `(a ` `-` `1` `) ` `+` `(n ` `-` `b ` `+` `1` `) ` ` ` ` ` `# Minimum distance between a and b ` ` ` `minDist ` `=` `min` `(distClock, distAntiClock) ` ` ` ` ` `# If both the positions are ` ` ` `# adjacent on the circle ` ` ` `if` `(minDist ` `=` `=` `1` `): ` ` ` `return` `3` ` ` ` ` `# Return the minimum Z possible ` ` ` `return` `minDist ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `4` ` ` `a ` `=` `1` ` ` `b ` `=` `2` ` ` `print` `(findMinimumZ(n, a, b)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum value of Z ` ` ` `static` `int` `findMinimumZ(` `int` `n, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `// Change elements such that a < b ` ` ` `if` `(a > b) ` ` ` `{ ` ` ` `swap(a, b); ` ` ` `} ` ` ` ` ` `// Distance from (a to b) ` ` ` `int` `distClock = b - a; ` ` ` ` ` `// Distance from (1 to a) + (b to n) ` ` ` `int` `distAntiClock = (a - 1) + (n - b + 1); ` ` ` ` ` `// Minimum distance between a and b ` ` ` `int` `minDist = Math.Min(distClock, distAntiClock); ` ` ` ` ` `// If both the positions are ` ` ` `// adjacent on the circle ` ` ` `if` `(minDist == 1) ` ` ` `{ ` ` ` `return` `3; ` ` ` `} ` ` ` ` ` `// Return the minimum Z possible ` ` ` `return` `minDist; ` ` ` `} ` ` ` ` ` `private` `static` `void` `swap(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 4, a = 1, b = 2; ` ` ` `Console.WriteLine(findMinimumZ(n, a, b)); ` ` ` `} ` ` ` `} ` ` ` `/* This code contributed by ajit*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of the approach ` `// Function to return the minimum value of Z ` `function` `findMinimumZ(` `$n` `, ` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// Change elements such that a < b ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` ` ` `$a` `= ` `$a` `^ ` `$b` `; ` ` ` `$b` `= ` `$a` `^ ` `$b` `; ` ` ` `$a` `= ` `$a` `^ ` `$b` `; ` ` ` ` ` `// Distance from (a to b) ` ` ` `$distClock` `= ` `$b` `- ` `$a` `; ` ` ` ` ` `// Distance from (1 to a) + (b to n) ` ` ` `$distAntiClock` `= (` `$a` `- 1) + (` `$n` `- ` `$b` `+ 1); ` ` ` ` ` `// Minimum distance between a and b ` ` ` `$minDist` `= min(` `$distClock` `, ` `$distAntiClock` `); ` ` ` ` ` `// If both the positions are ` ` ` `// adjacent on the circle ` ` ` `if` `(` `$minDist` `== 1) ` ` ` `return` `3; ` ` ` ` ` `// Return the minimum Z possible ` ` ` `return` `$minDist` `; ` `} ` ` ` `// Driver code ` ` ` `$n` `= 4; ` `$a` `= 1; ` `$b` `= 2; ` `echo` `findMinimumZ(` `$n` `, ` `$a` `, ` `$b` `); ` ` ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Minimum distance from a point to the line segment using Vectors
- Find the integer points (x, y) with Manhattan distance atleast N
- Find integral points with minimum distance from given set of integers using BFS
- Binary Array Range Queries to find the minimum distance between two Zeros
- Find the subarray of size K with minimum XOR
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Distance between a point and a Plane in 3 D
- Shortest distance between a point and a circle
- Perpendicular distance between a point and a Line in 2 D
- Shortest distance between a Line and a Point in a 3-D plane
- Minimum decrements to make integer A divisible by integer B
- Sort an Array of Points by their distance from a reference Point
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m

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.