- Related Questions & Answers
- Find the minimum distance between two numbers in C++
- Program to find minimum difference between two elements from two lists in Python
- Find minimum difference between any two element in C++
- How to find absolute difference between two numbers in MySQL?
- Program to find the maximum difference between the index of any two different numbers in C++
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Find the Symmetric difference between two arrays - JavaScript
- Find difference between sums of two diagonals in C++.
- How to find missing value between two MySQL Tables?
- Find the compatibility difference between two arrays in C++
- Find the difference between two datetime values with MySQL?
- How to find minimum between 2 numbers using C#?
- Find LCM of two numbers
- Find GCD of two numbers
- Find the difference between two timestamps in days with MySQL

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

Suppose we have two numbers p and q, we have to find the minimum difference between any terms in the shifted infinite tables of p and q, these shifts are r and s, where r, s >= 0.

So, if the input is like p = 7 and q = 17, r = 6 and s = 3, then the output will be 0 as, table of 7 = [7, 14, 21, 28, 35, 42, 49, ...] and table of 17 = [17, 34, 51, 68, 85, 102, 119, ...], then shifted table of 7 will be [13, 20, 27, 34, 41, 48, 55, ...] and shifted table of 17 will be [20, 37, 54, 71, 88, 105, 121, ...], then the minimum difference between two terms of these shifted tables are 20-20 = 0.

To solve this, we will follow these steps −

g := gcd of (p, q)

difference := |r-s| mod g

return minimum of difference and g - difference

Let us see the following implementation to get better understanding −

import math def get_minimum_diff (p, q, r, s): g = math.gcd(p,q) difference = abs(r-s) % g return min(difference, g - difference) p = 7 q = 17 r = 6 s = 3 print(get_minimum_diff(p, q, r, s))

7,17,6,3

0

Advertisements