Related Articles
Largest number by which given 3 numbers should be divided such that they leaves same remainder
• Difficulty Level : Easy
• Last Updated : 07 Apr, 2021

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++

 // C++ program to find the largest numbers that// leads to same remainder when divides given// three sorted numbers#include using namespace std; //__gcd functionint 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 programint main(){    int a = 62, b = 132, c = 237;    cout << sameRemainder(a, b, c) << endl;    return 0;}

## Java

 // Java program to find the largest// numbers that leads to same// remainder when divides given// three sorted numbers class GFG {     //__gcd functionstatic 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 codepublic 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.

## Python3

 # Python program to find# the largest numbers that# leads to same remainder# when divides given# three sorted numbers #__gcd functiondef 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 = 62b = 132c = 237print(sameRemainder(a, b, c)) # This code is contributed# by Anant Agarwal.

## C#

 // C# program to find the largest// numbers that leads to same// remainder when divides given// three sorted numbersusing System; class GFG {     // gcd functionstatic 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 codepublic static void Main(){    int a = 62, b = 132, c = 237;    Console.WriteLine(sameRemainder(a, b, c));}} // This code is contributed by vt_m.



## Javascript



Output:

35

Time Complexity: O( log N )

Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up