Largest number by which given 3 numbers should be divided such that they leaves same remainder

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
Brute Force Approach:
The brute force approach to solve this problem is to start from the largest number among the three (i.e., c) and decrement it until we find a number that leaves the same remainder when divided by a and b.
We can find the remainder of a, b, and c when divided by any number n using the modulo operator (%). Then, we can check if the remainders of a, b, and c when divided by a candidate number n are equal. If they are equal, then n is a valid solution.
Below is the implementation of above approach:
C++
// C++ program to find the largest numbers that// leads to same remainder when divides given// three sorted numbers#include <bits/stdc++.h>using namespace std;Â
// function return number which divides these// three number and leaves same remainder .int sameRemainder(int a, int b, int c){    // start from largest number and decrement until we find    // a solution    for (int n = c; n >= 1; n--) {        int remainderA = a % n;        int remainderB = b % n;        int remainderC = c % n;        if (remainderA == remainderB            && remainderB == remainderC) {            return n;        }    }    // no solution found    return -1;}Â
// 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 numbersimport java.util.*;Â
public class Main {Â
    // function return number which divides these    // three number and leaves same remainder .    static int sameRemainder(int a, int b, int c)    {        // start from largest number and decrement until we        // find a solution        for (int n = c; n >= 1; n--) {            int remainderA = a % n;            int remainderB = b % n;            int remainderC = c % n;            if (remainderA == remainderB                && remainderB == remainderC) {                return n;            }        }        // no solution found        return -1;    }Â
    // driver program    public static void main(String[] args)    {        int a = 62, b = 132, c = 237;        System.out.println(sameRemainder(a, b, c));    }} |
Python3
# Python program to find the largest numbers that# leads to same remainder when divides given# three sorted numbersÂ
Â
def same_remainder(a, b, c):    # start from largest number and decrement until we find a solution    for n in range(c, 0, -1):        remainder_a = a % n        remainder_b = b % n        remainder_c = c % n        if remainder_a == remainder_b == remainder_c:            return n    # no solution found    return -1Â
Â
a = 62b = 132c = 237print(same_remainder(a, b, c)) |
C#
using System;Â
class Program {Â
  // function return number which divides these  // three number and leaves same remainder .  static int SameRemainder(int a, int b, int c)  {Â
    // start from largest number and decrement until we    // find a solution    for (int n = c; n >= 1; n--) {      int remainderA = a % n;      int remainderB = b % n;      int remainderC = c % n;      if (remainderA == remainderB          && remainderB == remainderC) {        return n;      }    }Â
    // no solution found    return -1;  }Â
  static void Main(string[] args)  {    int a = 62, b = 132, c = 237;    Console.WriteLine(SameRemainder(a, b, c));  }} |
Javascript
function sameRemainder(a, b, c) {    // start from largest number and decrement until we find a solution    for (let n = c; n >= 1; n--) {        let remainderA = a % n;        let remainderB = b % n;        let remainderC = c % n;        if (remainderA == remainderB && remainderB == remainderC) {            return n;        }    }    // no solution found    return -1;}Â
// driver programlet a = 62, b = 132, c = 237;console.log(sameRemainder(a, b, c)); |
35
Time Complexity: O(c)
Auxiliary Space: O(1)
Approach:
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 <bits/stdc++.h>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 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. |
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 = 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 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. |
PHP
<?php// PHP program to find the// largest numbers that leads// to same remainder when // divides given three sorted// numbersÂ
//__gcd functionfunction 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.?> |
Javascript
<script>Â
// Javascript program to find the largest // numbers that leads to same remainder // when divides given three sorted numbersÂ
//__gcd functionfunction 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    var a1 = (b - a), b1 = (c - b), c1 = (c - a);Â
    // Return GCD of three differences.    return gcd(a1, gcd(b1, c1));}Â
// Driver codevar a = 62, b = 132, c = 237;Â
document.write(sameRemainder(a, b, c));Â
// This code is contributed by noob2000Â
</script> |
35
Time Complexity: O( log N )
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



