- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Suppose we have a ring, which is made of few numbers from 1 to N. We also have tow numbers A and B. Now, we can stand at any place (say x) and perform the count operation with respect of the total sum of the distance (say Z = distance from X to A + distance from X to B). We have to select X such that Z is minimized. At the end return the value of Z. We have to keep in mind that X will not be same as A and B.

So, if the input is like N = 30, A = 10, B = 20, then the output will be 10 as by selecting X = 15, distance from X to A is 5, and distance from X to B is 5. So, total distance = 5 + 5 = 10.

To solve this, we will follow these steps −

if a > b is non-zero, then

swap a and b

clock_wise_dist := b - a

counter_clock_wise_dist :=(a - 1) +(n - b + 1)

minimum_dist := minimum of clock_wise_dist, counter_clock_wise_dist

if minimum_dist is same as 1, then

return 3

return minimum_dist

## Example

Let us see the following implementation to get better understanding −

def get_min_z(n, a, b): if (a > b): a, b = b, a clock_wise_dist = b - a counter_clock_wise_dist = (a - 1) + (n - b + 1) minimum_dist = min(clock_wise_dist, counter_clock_wise_dist) if (minimum_dist == 1): return 3 return minimum_dist n = 30 a = 10 b = 20 print(get_min_z(n, a, b))

## Input

30, 10, 20

## Output

10

- Related Questions & Answers
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Program to find size of sublist where product of minimum of A and size of A is maximized in Python
- Find maximum (or minimum) sum of a subarray of size k in C++
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B in C++
- Program to find sum of minimum trees from the list of leaves in python
- Program to find minimum distance of two given words in a text in Python
- Find A and B from list of divisors in C++
- Find a number which give minimum sum when XOR with every number of array of integer in Python
- Program to find out the minimum size of the largest clique in a graph (Python)
- Check if N can be represented as sum of integers chosen from set {A, B} in Python
- Program to find largest sum of any path of a binary tree in Python
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++