Given three numbers, our task is to find the largest number by which when given 3 numbers are divided leads to same remainder. It may be assumed that all given numbers are given in increasing order.

Examples:

Input : a = 62, b = 132, c = 237 Output : 35 35 leads to same remainder 27 when divides 62, 132 and 237. Input : a = 74, b = 272, c = 584 Output : 6

The idea is based on the fact that if a number leaves same remainder with a, b and c, then it would divide their differences. Let us understand assuming that x is our result. Let **a = x*d1 + r** where r is the remainder when a is divided by x. Similarly we can write **b = x*d2 + r** and **b = x*d3 + r**. So the logic is here we first find differences of all three pairs and after that, we find greatest common divisor of differences to maximize result.

Below is the implementation of above idea.

## C++

`// CPP program to find the largest numbers that ` `// leads to same remainder when divides given ` `// three sorted numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `//__gcd function ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// function return number which divides these ` `// three number and leaves same remainder . ` `int` `sameRemainder(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `// We find the differences of all three pairs ` ` ` `int` `a1 = (b - a), b1 = (c - b), c1 = (c - a); ` ` ` ` ` `// Return GCD of three differences. ` ` ` `return` `gcd(a1, gcd(b1, c1)); ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `a = 62, b = 132, c = 237; ` ` ` `cout << sameRemainder(a, b, c) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the largest ` `// numbers that leads to same ` `// remainder when divides given ` `// three sorted numbers ` ` ` `class` `GFG { ` ` ` `//__gcd function ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// function return number which divides these ` `// three number and leaves same remainder . ` `static` `int` `sameRemainder(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `// We find the differences of all three pairs ` ` ` `int` `a1 = (b - a), b1 = (c - b), c1 = (c - a); ` ` ` ` ` `// Return GCD of three differences. ` ` ` `return` `gcd(a1, gcd(b1, c1)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `62` `, b = ` `132` `, c = ` `237` `; ` ` ` `System.out.println(sameRemainder(a, b, c)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# the largest numbers that ` `# leads to same remainder ` `# when divides given ` `# three sorted numbers ` ` ` `#__gcd function ` `def` `gcd(a,b): ` ` ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `# function return number ` `# which divides these ` `# three number and leaves ` `# same remainder . ` `def` `sameRemainder(a,b,c): ` ` ` ` ` `# We find the differences ` ` ` `# of all three pairs ` ` ` `a1 ` `=` `(b ` `-` `a) ` ` ` `b1 ` `=` `(c ` `-` `b) ` ` ` `c1 ` `=` `(c ` `-` `a) ` ` ` ` ` `# Return GCD of three differences. ` ` ` `return` `gcd(a1, gcd(b1, c1)) ` ` ` `# Driver program ` ` ` `a ` `=` `62` `b ` `=` `132` `c ` `=` `237` `print` `(sameRemainder(a, b, c)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the largest ` `// numbers that leads to same ` `// remainder when divides given ` `// three sorted numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// gcd function ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// function return number which divides ` `// these three number and leaves same ` `// remainder . ` `static` `int` `sameRemainder(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `// We find the differences of all three pairs ` ` ` `int` `a1 = (b - a), b1 = (c - b), c1 = (c - a); ` ` ` ` ` `// Return GCD of three differences. ` ` ` `return` `gcd(a1, gcd(b1, c1)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 62, b = 132, c = 237; ` ` ` `Console.WriteLine(sameRemainder(a, b, c)); ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// largest numbers that leads ` `// to same remainder when ` `// divides given three sorted ` `// numbers ` ` ` `//__gcd function ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// function return number ` `// which divides these ` `// three number and ` `// leaves same remainder . ` `function` `sameRemainder( ` `$a` `, ` `$b` `, ` `$c` `) ` `{ ` ` ` ` ` `// We find the differences ` ` ` `// of all three pairs ` ` ` `$a1` `= (` `$b` `- ` `$a` `); ` ` ` `$b1` `= (` `$c` `- ` `$b` `); ` ` ` `$c1` `= (` `$c` `- ` `$a` `); ` ` ` ` ` `// Return GCD of ` ` ` `// three differences. ` ` ` `return` `gcd(` `$a1` `, gcd(` `$b1` `, ` `$c1` `)); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$a` `= 62; ` ` ` `$b` `= 132; ` ` ` `$c` `= 237; ` ` ` `echo` `sameRemainder(` `$a` `, ` `$b` `, ` `$c` `) ; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

35

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.

## Recommended Posts:

- Find remainder of array multiplication divided by n
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- First N natural can be divided into two sets with given difference and co-prime sums
- First number to leave an odd remainder after repetitive division by 2
- Check if a large number can be divided into two or more segments of equal sum
- Largest number N which can be reduced to 0 in K steps
- Largest N digit number divisible by given three numbers
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Largest number M less than N such that XOR of M and N is even
- Check if Euler Totient Function is same for a given number and twice of that number
- Largest Even and Odd N-digit numbers in Octal Number System
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Find largest factor of N such that N/F is less than K
- Count Numbers with N digits which consists of even number of 0’s
- Split a number as sum of K numbers which are not divisible by K
- Given two numbers a and b find all x such that a % x = b
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K

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.