GFG App
Open App
Browser
Continue

# Find M for which numbers A, B, C form an A.P. if any one is divided by M

Given three positive integers A, B, and C, the task is to find out that, if we divide any one of them with any integer M(m>0), can they form an A.P.(Arithmetic Progression) in the same given order. If there are multiple values possible then print all of them and if no value is possible then print -1.

Examples:

Input:  A = 25, B = 10, C = 15
Output:  5
Explanation: If A(25) is divided by 5 then the three integers are 5, 10, 15 which form an A.P
with common difference 5.

Input:  A = 18, B = 4, C = 2
Output:  3.
Explanation: If A(18) is divided by 3 then the three integers are 6, 4, 2 which form an A.P
with common difference -2.

Input:  A = 7, B = 11, C = 13
Output:  -1
Explanation: It can be proved that there exists no positive integer which on dividing with
any one of  the three numbers can form an A.P.

Approach:

The three numbers A, B, and C are in A.P. if-

B – A = C – B = d

Here,
A, B, and C are the three numbers
d is the common difference

Using the above common difference property of A.P. there can be three formulas for three cases-

• When A is divided by an integer m1-

For A / m1, B, C  to form an A.P., we have

B – A / m1 = C – B

Thus, m1 = a / (2 * B – C)

• When B is divided by an integer m2-

For A, B / m2, C  to form an A.P., we have

B / m2 – A = C – B / m2

Thus, m2 = 2 * B/ (C + A)

• When C is divided by an integer m3-

For A, B, C / m3  to form an A.P., we have

B – A = C / m3 – B

Thus, m3 = C / (2 * B – A)

• Now we have possible values of M for each case, then check for each of the three values if any one of them is a positive integer, then that value is the required answer.
• If no such possible value exists then print -1.

Below is the implementation for the above approach-

## C++

 `// C++ code to implement the given approach` `#include ` `using` `namespace` `std;`   `// Utility function to check` `// if given argument is an integer or not` `bool` `ifint(``double` `x)` `{` `    ``int` `a = x;`   `    ``if` `(x - a > 0)` `        ``return` `false``;` `    ``else` `        ``return` `true``;` `}`   `// Function to find any integer M if exists` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `{` `    ``double` `m1 = (``double``)(A / (2 * B - C));` `    ``double` `m2 = (``double``)(2 * B / (C + A));` `    ``double` `m3 = (``double``)(C / (2 * B - A));`   `    ``// Checks if it is both` `    ``// positive and an integer` `    ``if` `(m1 > 1 && ifint(m1))` `        ``cout << m1;`   `    ``else` `if` `(m2 > 1 && ifint(m2))` `        ``cout << m2;`   `    ``else` `if` `(m3 > 1 && ifint(m3))` `        ``cout << m3;`   `    ``else` `        ``cout << ``"-1"``;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `A = 2;` `    ``int` `B = 4;` `    ``int` `C = 18;`   `    ``findVal(A, B, C);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Utility function to check` `  ``// if given argument is an integer or not` `  ``static` `Boolean ifint(``double` `x)` `  ``{` `    ``int` `a = (``int``)x;`   `    ``if` `(x - a > ``0``)` `      ``return` `false``;` `    ``else` `      ``return` `true``;` `  ``}`   `  ``// Function to find any integer M if exists` `  ``static` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `  ``{` `    ``double` `m1 = (``double``)(A / (``2` `* B - C));` `    ``double` `m2 = (``double``)(``2` `* B / (C + A));` `    ``double` `m3 = (``double``)(C / (``2` `* B - A));`   `    ``// Checks if it is both` `    ``// positive and an integer` `    ``if` `(m1 > ``1` `&& ifint(m1)){` `      ``int` `M1 = (``int``)m1;` `      ``System.out.print(M1);` `    ``}`   `    ``else` `if` `(m2 > ``1` `&& ifint(m2)){` `      ``int` `M2 = (``int``)m2;` `      ``System.out.print(M2);` `    ``}`   `    ``else` `if` `(m3 > ``1` `&& ifint(m3)){` `      ``int` `M3 = (``int``)m3;` `      ``System.out.print(M3);` `    ``}`   `    ``else` `      ``System.out.print(``"-1"``);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main (String[] args) {` `    ``int` `A = ``2``;` `    ``int` `B = ``4``;` `    ``int` `C = ``18``;`   `    ``findVal(A, B, C);    ` `  ``}` `}`   `// This code is contributed by hrithikgarg03188/`

## Python

 `# Python code to implement the given approach`   `# Utility function to check` `# if given argument is an integer or not` `def` `ifint(x):` `    ``a ``=` `x`   `    ``if` `(x ``-` `a > ``0``):` `        ``return` `False` `    ``else``:` `        ``return` `True`   `# Function to find any integer M if exists` `def` `findVal(A, B, C):` `    `  `    ``m1 ``=` `(A ``/` `(``2` `*` `B ``-` `C))` `    ``m2 ``=` `(``2` `*` `B ``/` `(C ``+` `A))` `    ``m3 ``=` `(C ``/` `(``2` `*` `B ``-` `A));`   `    ``# Checks if it is both` `    ``# positive and an integer` `    ``if` `(m1 > ``1` `and` `ifint(m1)):` `        ``print``(m1)`   `    ``elif` `(m2 > ``1` `and` `ifint(m2)):` `        ``print``(m2)`   `    ``elif` `(m3 > ``1` `and` `ifint(m3)):` `        ``print``(m3)`   `    ``else``:` `        ``print``(``-``1``)`   `# Driver code` `A ``=` `2` `B ``=` `4` `C ``=` `18`   `findVal(A, B, C)`   `# This code is contributed by Samim Hossain Mondal.`

## C#

 `// C# code to implement the given approach` `using` `System;` `class` `GFG {` `    ``// Utility function to check` `    ``// if given argument is an integer or not` `    ``static` `bool` `ifint(``double` `x)` `    ``{` `        ``double` `a = x;`   `        ``if` `(x - a > 0)` `            ``return` `false``;` `        ``else` `            ``return` `true``;` `    ``}`   `    ``// Function to find any integer M if exists` `    ``static` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `    ``{` `        ``double` `m1 = Convert.ToDouble(A / (2 * B - C));` `        ``double` `m2 = Convert.ToDouble(2 * B / (C + A));` `        ``double` `m3 = Convert.ToDouble(C / (2 * B - A));`   `        ``// Checks if it is both` `        ``// positive and an integer` `        ``if` `(m1 > 1 && ifint(m1))` `            ``Console.Write(m1);`   `        ``else` `if` `(m2 > 1 && ifint(m2))` `            ``Console.Write(m2);`   `        ``else` `if` `(m3 > 1 && ifint(m3))` `            ``Console.Write(m3);`   `        ``else` `            ``Console.Write(-1);` `    ``}`   `    ``// Driver code` `    ``public` `static` `int` `Main()` `    ``{` `        ``int` `A = 2;` `        ``int` `B = 4;` `        ``int` `C = 18;`   `        ``findVal(A, B, C);` `        ``return` `0;` `    ``}` `}`   `// This code is contributed by Taranpreet`

## Javascript

 ``

Output

`3`

Time Complexity: O(1)
Auxiliary Space: O(1)

## C++

 `// C++ code to implement the given approach` `#include ` `using` `namespace` `std;`   `// Utility function to check` `// if given argument is an integer or not` `bool` `ifint(``double` `x)` `{` `    ``int` `a = x;`   `    ``if` `(x - a > 0)` `        ``return` `false``;` `    ``else` `        ``return` `true``;` `}`   `// Function to find any integer M if exists` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `{` `    ``double` `m1 = (``double``)(A / (2 * B - C));` `    ``double` `m2 = (``double``)(2 * B / (C + A));` `    ``double` `m3 = (``double``)(C / (2 * B - A));`   `    ``// Checks if it is both` `    ``// positive and an integer` `    ``if` `(m1 > 1 && ifint(m1))` `        ``cout << m1;`   `    ``else` `if` `(m2 > 1 && ifint(m2))` `        ``cout << m2;`   `    ``else` `if` `(m3 > 1 && ifint(m3))` `        ``cout << m3;`   `    ``else` `        ``cout << ``"-1"``;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `A = 2;` `    ``int` `B = 4;` `    ``int` `C = 18;`   `    ``findVal(A, B, C);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Utility function to check` `  ``// if given argument is an integer or not` `  ``static` `Boolean ifint(``double` `x)` `  ``{` `    ``int` `a = (``int``)x;`   `    ``if` `(x - a > ``0``)` `      ``return` `false``;` `    ``else` `      ``return` `true``;` `  ``}`   `  ``// Function to find any integer M if exists` `  ``static` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `  ``{` `    ``double` `m1 = (``double``)(A / (``2` `* B - C));` `    ``double` `m2 = (``double``)(``2` `* B / (C + A));` `    ``double` `m3 = (``double``)(C / (``2` `* B - A));`   `    ``// Checks if it is both` `    ``// positive and an integer` `    ``if` `(m1 > ``1` `&& ifint(m1)){` `      ``int` `M1 = (``int``)m1;` `      ``System.out.print(M1);` `    ``}`   `    ``else` `if` `(m2 > ``1` `&& ifint(m2)){` `      ``int` `M2 = (``int``)m2;` `      ``System.out.print(M2);` `    ``}`   `    ``else` `if` `(m3 > ``1` `&& ifint(m3)){` `      ``int` `M3 = (``int``)m3;` `      ``System.out.print(M3);` `    ``}`   `    ``else` `      ``System.out.print(``"-1"``);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main (String[] args) {` `    ``int` `A = ``2``;` `    ``int` `B = ``4``;` `    ``int` `C = ``18``;`   `    ``findVal(A, B, C);    ` `  ``}` `}`   `// This code is contributed by hrithikgarg03188/`

## Python3

 `# Python code for the above approach `   `# Utility function to check` `# if given argument is an integer or not` `def` `ifint(x):` `    ``a ``=` `x;`   `    ``if` `(x ``-` `a > ``0``):` `        ``return` `False``;` `    ``else``:` `        ``return` `True``;`   `# Function to find any integer M if exists` `def` `findVal(A, B, C):` `    ``m1 ``=` `(A ``/` `(``2` `*` `B ``-` `C));` `    ``m2 ``=` `(``2` `*` `B ``/` `(C ``+` `A));` `    ``m3 ``=` `(C ``/` `(``2` `*` `B ``-` `A));`   `    ``# Checks if it is both` `    ``# positive and an integer` `    ``if` `(m1 > ``1` `and` `ifint(m1)):` `        ``print``(``int``(m1))`   `    ``elif` `(m2 > ``1` `and` `ifint(m2)):` `        ``print``(``int``(m2))`   `    ``elif` `(m3 > ``1` `and` `ifint(m3)):` `        ``print``(``int``(m3))`   `    ``else``:` `        ``print``(``"-1"``);`   `# Driver code` `A ``=` `2``;` `B ``=` `4``;` `C ``=` `18``;`   `findVal(A, B, C);`   `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# code to implement the given approach` `using` `System;` `class` `GFG ` `{` `  `  `    ``// Utility function to check` `    ``// if given argument is an integer or not` `    ``static` `bool` `ifint(``double` `x)` `    ``{` `        ``double` `a = x;`   `        ``if` `(x - a > 0)` `            ``return` `false``;` `        ``else` `            ``return` `true``;` `    ``}`   `    ``// Function to find any integer M if exists` `    ``static` `void` `findVal(``int` `A, ``int` `B, ``int` `C)` `    ``{` `        ``double` `m1 = Convert.ToDouble(A / (2 * B - C));` `        ``double` `m2 = Convert.ToDouble(2 * B / (C + A));` `        ``double` `m3 = Convert.ToDouble(C / (2 * B - A));`   `        ``// Checks if it is both` `        ``// positive and an integer` `        ``if` `(m1 > 1 && ifint(m1))` `            ``Console.Write(m1);`   `        ``else` `if` `(m2 > 1 && ifint(m2))` `            ``Console.Write(m2);`   `        ``else` `if` `(m3 > 1 && ifint(m3))` `            ``Console.Write(m3);`   `        ``else` `            ``Console.Write(-1);` `    ``}`   `    ``// Driver code` `    ``public` `static` `int` `Main()` `    ``{` `        ``int` `A = 2;` `        ``int` `B = 4;` `        ``int` `C = 18;`   `        ``findVal(A, B, C);` `        ``return` `0;` `    ``}` `}`   `// This code is contributed by Taranpreet`

## Javascript

 ``

Time Complexity: O(1).

Space Complexity: O(1) as no extra space has been used.

My Personal Notes arrow_drop_up