# Find M for which A, B, C form an A.P. in given order if any one is multiplied by M

Given 3 positive integers **A, B, and C. **Choose a positive integer **M** and multiply any one of A, B, or C with M. The task is to decide whether A, B and C would comply with Arithmetic Progression Mean (A.P.) Mean after performing the above-given operation once. The order of A, B, C cannot be changed.

**Examples:**

Input:A = 30, B = 5, C = 10Output:YESExplanation:B = 5 can be multiplied by M = 4.

Then 2 * B = A + C (2 * 20 = 30 + 10).

Input:A = 2, B = 1, C = 1Output:NOExplanation:A + C = 3 which is odd. B = 1 which is odd.

Both these values cannot be made even (because 2*B is always even so need for making both of them even) and equal simultaneously.

**Approach: **

- Three integers are said to be in A.P. if

2 * B = A + C -(1)This equation can also be written as

2 * B – C = A -(2)and

2 * B – A = C -(3)

Below are the steps that use this relationship to determine whether the given three numbers can form an A.P. or not-

- Declare a variable
**new_b**and initiate it equal to**2 * B**. - Return
**true**if**A, B, C**are already**in AP**. Any of them can be multiplied by M=1 and the answer will always be “**YES**“. - Return
**true**if B can be multiplied by any number to make it equal to A+C i.e.**(A + C) % new_b = 0**. The remainder will always be 0 irrespective of the value of**M**. - Now the only options remaining are to multiply either
**A**or**C**to make their sum equal to**new_b**.- If
**(A + C) > new_b,**return**false**. In this case, multiplying**A**or**C**with**M**will only increase its value. The answer will always be “**NO**“. - Else if
**(new_b – C) % A = 0**, return**true**. In this case,**A**can be multiplied by some integer**M**. In this case,**(new_b – C)****A**. Hence, on dividing**(new_b – C)**by**A**, the remainder will always be**0**irrespective of the value of M. The answer will be “**YES**” (a per equation (2)). - Else if
**(new_b – A) % B = 0**, return**true**. In this case,**C**can be multiplied by some integer**M**. In this case,**(new_b – A)****C**. Hence, on dividing**(new_b – A)**by**C**, the remainder will always be**0**irrespective of the value of**M**. The answer will be “**YES**” (as per equation (3)). - Else return
**false**.

- If

**Illustration:**

Input:A = 30, B = 5, C = 10Output:YESExplanation:

new_b = 2 * B

= 2 * 5

= 10

(A + C) % new_b

(30 + 10) % 10

40 % 10 = 0

This proves that the numbers30 5 10are inA.P.after multiplying with some number M.

Below is the implementation for the above approach-

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if A, B, C` `// can be in A.P. after multiplying` `// by any integer M` `bool` `isAP(` `int` `A, ` `int` `B, ` `int` `C)` `{` ` ` `int` `new_b = B * 2;` ` ` `// Check if A, B, C are already` ` ` `// in A.P.` ` ` `if` `(new_b == (A + C))` ` ` `return` `true` `;` ` ` `// Check if multiplying B will` ` ` `// give A.P.` ` ` `if` `((A + C) % new_b == 0)` ` ` `return` `true` `;` ` ` `if` `((A + C) > new_b)` ` ` `return` `false` `;` ` ` `// Check if multiplying A will` ` ` `// give A.P.` ` ` `if` `((new_b - C) % A == 0)` ` ` `return` `true` `;` ` ` `// Check if multiplying C will` ` ` `// give A.P.` ` ` `if` `((new_b - A) % C == 0)` ` ` `return` `true` `;` ` ` `return` `false` `;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `A, B, C;` ` ` `A = 30;` ` ` `B = 5;` ` ` `C = 10;` ` ` `// Function call` ` ` `bool` `ans = isAP(A, B, C);` ` ` `// Displaying the answer` ` ` `if` `(ans)` ` ` `cout << ` `"YES"` `;` ` ` `else` ` ` `cout << ` `"NO"` `;` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;` `class` `GFG {` ` ` `// Function to check if A, B, C` ` ` `// can be in A.P. after multiplying` ` ` `// by any integer M` ` ` `static` `Boolean isAP(` `int` `A, ` `int` `B, ` `int` `C)` ` ` `{` ` ` `int` `new_b = B * ` `2` `;` ` ` `// Check if A, B, C are already` ` ` `// in A.P.` ` ` `if` `(new_b == (A + C))` ` ` `return` `true` `;` ` ` `// Check if multiplying B will` ` ` `// give A.P.` ` ` `if` `((A + C) % new_b == ` `0` `)` ` ` `return` `true` `;` ` ` `if` `((A + C) > new_b)` ` ` `return` `false` `;` ` ` `// Check if multiplying A will` ` ` `// give A.P.` ` ` `if` `((new_b - C) % A == ` `0` `)` ` ` `return` `true` `;` ` ` `// Check if multiplying C will` ` ` `// give A.P.` ` ` `if` `((new_b - A) % C == ` `0` `)` ` ` `return` `true` `;` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main (String[] args) {` ` ` `int` `A, B, C;` ` ` `A = ` `30` `;` ` ` `B = ` `5` `;` ` ` `C = ` `10` `;` ` ` `// Function call` ` ` `Boolean ans = isAP(A, B, C);` ` ` `// Displaying the answer` ` ` `if` `(ans)` ` ` `System.out.print(` `"YES"` `);` ` ` `else` ` ` `System.out.print(` `"NO"` `);` ` ` `}` `}` `// This code is contributed by hrithikgarg03188` |

## Python3

`# Python code for the above approach` `# Function to check if A, B, C` `# can be in A.P. after multiplying` `# by any integer M` `def` `isAP(A, B, C):` ` ` `new_b ` `=` `B ` `*` `2` ` ` `# Check if A, B, C are already` ` ` `# in A.P.` ` ` `if` `(new_b ` `=` `=` `(A ` `+` `C)):` ` ` `return` `True` ` ` `# Check if multiplying B will` ` ` `# give A.P.` ` ` `if` `((A ` `+` `C) ` `%` `new_b ` `=` `=` `0` `):` ` ` `return` `True` ` ` `if` `((A ` `+` `C) > new_b):` ` ` `return` `False` ` ` `# Check if multiplying A will` ` ` `# give A.P.` ` ` `if` `((new_b ` `-` `C) ` `%` `A ` `=` `=` `0` `):` ` ` `return` `True` ` ` `# Check if multiplying C will` ` ` `# give A.P.` ` ` `if` `((new_b ` `-` `A) ` `%` `C ` `=` `=` `0` `):` ` ` `return` `True` ` ` `return` `False` `# Driver code` `A ` `=` `30` `B ` `=` `5` `C ` `=` `10` `# Function call` `ans ` `=` `isAP(A, B, C)` `# Displaying the answer` `if` `(ans):` ` ` `print` `(` `"YES"` `)` `else` `:` ` ` `print` `(` `"NO"` `)` `# This code is contributed by Saurabh Jaiswal` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to check if A, B, C` ` ` `// can be in A.P. after multiplying` ` ` `// by any integer M` ` ` `static` `bool` `isAP(` `int` `A, ` `int` `B, ` `int` `C)` ` ` `{` ` ` `int` `new_b = B * 2;` ` ` `// Check if A, B, C are already` ` ` `// in A.P.` ` ` `if` `(new_b == (A + C))` ` ` `return` `true` `;` ` ` `// Check if multiplying B will` ` ` `// give A.P.` ` ` `if` `((A + C) % new_b == 0)` ` ` `return` `true` `;` ` ` `if` `((A + C) > new_b)` ` ` `return` `false` `;` ` ` `// Check if multiplying A will` ` ` `// give A.P.` ` ` `if` `((new_b - C) % A == 0)` ` ` `return` `true` `;` ` ` `// Check if multiplying C will` ` ` `// give A.P.` ` ` `if` `((new_b - A) % C == 0)` ` ` `return` `true` `;` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `int` `Main()` ` ` `{` ` ` `int` `A, B, C;` ` ` `A = 30;` ` ` `B = 5;` ` ` `C = 10;` ` ` `// Function call` ` ` `bool` `ans = isAP(A, B, C);` ` ` `// Displaying the answer` ` ` `if` `(ans)` ` ` `Console.Write(` `"YES"` `);` ` ` `else` ` ` `Console.Write(` `"NO"` `);` ` ` `return` `0;` ` ` `}` `}` `// This code is contributed by Taranpreet` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach ` ` ` `// Function to check if A, B, C` ` ` `// can be in A.P. after multiplying` ` ` `// by any integer M` ` ` `function` `isAP(A, B, C)` ` ` `{` ` ` `let new_b = B * 2;` ` ` `// Check if A, B, C are already` ` ` `// in A.P.` ` ` `if` `(new_b == (A + C))` ` ` `return` `true` `;` ` ` `// Check if multiplying B will` ` ` `// give A.P.` ` ` `if` `((A + C) % new_b == 0)` ` ` `return` `true` `;` ` ` `if` `((A + C) > new_b)` ` ` `return` `false` `;` ` ` `// Check if multiplying A will` ` ` `// give A.P.` ` ` `if` `((new_b - C) % A == 0)` ` ` `return` `true` `;` ` ` `// Check if multiplying C will` ` ` `// give A.P.` ` ` `if` `((new_b - A) % C == 0)` ` ` `return` `true` `;` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver code` ` ` `let A, B, C;` ` ` `A = 30;` ` ` `B = 5;` ` ` `C = 10;` ` ` `// Function call` ` ` `let ans = isAP(A, B, C);` ` ` `// Displaying the answer` ` ` `if` `(ans)` ` ` `document.write(` `"YES"` `);` ` ` `else` ` ` `document.write(` `"NO"` `);` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

YES

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