Find M for which A, B, C form an A.P. in given order if any one is multiplied by M
Last Updated :
21 Feb, 2022
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 = 10
Output: YES
Explanation: B = 5 can be multiplied by M = 4.
Then 2 * B = A + C (2 * 20 = 30 + 10).
Input: A = 2, B = 1, C = 1
Output: NO
Explanation: 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) can be seen as a multiple of 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) can be seen as a multiple of 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.
Illustration:
Input: A = 30, B = 5, C = 10
Output: YES
Explanation:
new_b = 2 * B
= 2 * 5
= 10
(A + C) % new_b
(30 + 10) % 10
40 % 10 = 0
This proves that the numbers 30 5 10 are in A.P. after multiplying with some number M.
Below is the implementation for the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
bool isAP( int A, int B, int C)
{
int new_b = B * 2;
if (new_b == (A + C))
return true ;
if ((A + C) % new_b == 0)
return true ;
if ((A + C) > new_b)
return false ;
if ((new_b - C) % A == 0)
return true ;
if ((new_b - A) % C == 0)
return true ;
return false ;
}
int main()
{
int A, B, C;
A = 30;
B = 5;
C = 10;
bool ans = isAP(A, B, C);
if (ans)
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean isAP( int A, int B, int C)
{
int new_b = B * 2 ;
if (new_b == (A + C))
return true ;
if ((A + C) % new_b == 0 )
return true ;
if ((A + C) > new_b)
return false ;
if ((new_b - C) % A == 0 )
return true ;
if ((new_b - A) % C == 0 )
return true ;
return false ;
}
public static void main (String[] args) {
int A, B, C;
A = 30 ;
B = 5 ;
C = 10 ;
Boolean ans = isAP(A, B, C);
if (ans)
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def isAP(A, B, C):
new_b = B * 2
if (new_b = = (A + C)):
return True
if ((A + C) % new_b = = 0 ):
return True
if ((A + C) > new_b):
return False
if ((new_b - C) % A = = 0 ):
return True
if ((new_b - A) % C = = 0 ):
return True
return False
A = 30
B = 5
C = 10
ans = isAP(A, B, C)
if (ans):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool isAP( int A, int B, int C)
{
int new_b = B * 2;
if (new_b == (A + C))
return true ;
if ((A + C) % new_b == 0)
return true ;
if ((A + C) > new_b)
return false ;
if ((new_b - C) % A == 0)
return true ;
if ((new_b - A) % C == 0)
return true ;
return false ;
}
public static int Main()
{
int A, B, C;
A = 30;
B = 5;
C = 10;
bool ans = isAP(A, B, C);
if (ans)
Console.Write( "YES" );
else
Console.Write( "NO" );
return 0;
}
}
|
Javascript
<script>
function isAP(A, B, C)
{
let new_b = B * 2;
if (new_b == (A + C))
return true ;
if ((A + C) % new_b == 0)
return true ;
if ((A + C) > new_b)
return false ;
if ((new_b - C) % A == 0)
return true ;
if ((new_b - A) % C == 0)
return true ;
return false ;
}
let A, B, C;
A = 30;
B = 5;
C = 10;
let ans = isAP(A, B, C);
if (ans)
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...