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++
#include <bits/stdc++.h>
using namespace std;
bool ifint( double x)
{
int a = x;
if (x - a > 0)
return false ;
else
return true ;
}
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));
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" ;
}
int main()
{
int A = 2;
int B = 4;
int C = 18;
findVal(A, B, C);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean ifint( double x)
{
int a = ( int )x;
if (x - a > 0 )
return false ;
else
return true ;
}
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));
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" );
}
public static void main (String[] args) {
int A = 2 ;
int B = 4 ;
int C = 18 ;
findVal(A, B, C);
}
}
|
Python
def ifint(x):
a = x
if (x - a > 0 ):
return False
else :
return True
def findVal(A, B, C):
m1 = (A / ( 2 * B - C))
m2 = ( 2 * B / (C + A))
m3 = (C / ( 2 * B - A));
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 )
A = 2
B = 4
C = 18
findVal(A, B, C)
|
C#
using System;
class GFG {
static bool ifint( double x)
{
double a = x;
if (x - a > 0)
return false ;
else
return true ;
}
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));
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);
}
public static int Main()
{
int A = 2;
int B = 4;
int C = 18;
findVal(A, B, C);
return 0;
}
}
|
Javascript
<script>
function ifint(x) {
let a = x;
if (x - a > 0)
return false ;
else
return true ;
}
function findVal(A, B, C) {
let m1 = (A / (2 * B - C));
let m2 = (2 * B / (C + A));
let m3 = (C / (2 * B - A));
if (m1 > 1 && ifint(m1))
document.write(m1);
else if (m2 > 1 && ifint(m2))
document.write(m2);
else if (m3 > 1 && ifint(m3))
document.write(m3);
else
document.write( "-1" );
}
let A = 2;
let B = 4;
let C = 18;
findVal(A, B, C);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
C++
#include <bits/stdc++.h>
using namespace std;
bool ifint( double x)
{
int a = x;
if (x - a > 0)
return false ;
else
return true ;
}
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));
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" ;
}
int main()
{
int A = 2;
int B = 4;
int C = 18;
findVal(A, B, C);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean ifint( double x)
{
int a = ( int )x;
if (x - a > 0 )
return false ;
else
return true ;
}
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));
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" );
}
public static void main (String[] args) {
int A = 2 ;
int B = 4 ;
int C = 18 ;
findVal(A, B, C);
}
}
|
Python3
def ifint(x):
a = x;
if (x - a > 0 ):
return False ;
else :
return True ;
def findVal(A, B, C):
m1 = (A / ( 2 * B - C));
m2 = ( 2 * B / (C + A));
m3 = (C / ( 2 * B - A));
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" );
A = 2 ;
B = 4 ;
C = 18 ;
findVal(A, B, C);
|
C#
using System;
class GFG
{
static bool ifint( double x)
{
double a = x;
if (x - a > 0)
return false ;
else
return true ;
}
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));
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);
}
public static int Main()
{
int A = 2;
int B = 4;
int C = 18;
findVal(A, B, C);
return 0;
}
}
|
Javascript
<script>
function ifint(x) {
let a = x;
if (x - a > 0)
return false ;
else
return true ;
}
function findVal(A, B, C) {
let m1 = (A / (2 * B - C));
let m2 = (2 * B / (C + A));
let m3 = (C / (2 * B - A));
if (m1 > 1 && ifint(m1))
document.write(m1);
else if (m2 > 1 && ifint(m2))
document.write(m2);
else if (m3 > 1 && ifint(m3))
document.write(m3);
else
document.write( "-1" );
}
let A = 2;
let B = 4;
let C = 18;
findVal(A, B, C);
</script>
|
Time Complexity: O(1).
Space Complexity: O(1) as no extra space has been used.
Last Updated :
16 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...