Find two numbers whose sum and GCD are given
Last Updated :
29 Jun, 2022
Given sum and gcd of two numbers and . The task is to find both the numbers a and b. If the numbers do not exist then print .
Examples:
Input: sum = 6, gcd = 2
Output: a = 4, b = 2
4 + 2 = 6 and GCD(4, 2) = 2
Input: sum = 7, gcd = 2
Output: -1
There are no such numbers whose sum is 7 and GCD is 2
Approach: As GCD is given then it is known that both the numbers will be multiples of it.
- Choose the first number as gcd then the other number will be sum – gcd.
- If the sum of both the numbers chosen in the previous step equals to sum then print both the numbers.
- Else the numbers do not exist and print -1 instead.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findTwoNumbers( int sum, int gcd)
{
if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
cout << "a = " << min(gcd, sum - gcd)
<< ", b = " << sum - min(gcd, sum - gcd)
<< endl;
else
cout << -1 << endl;
}
int main()
{
int sum = 8;
int gcd = 2;
findTwoNumbers(sum, gcd);
return 0;
}
|
Java
import java.util.*;
class Solution{
static int __gcd( int a, int b)
{
if (b== 0 ) return a;
return __gcd(b,a%b);
}
static void findTwoNumbers( int sum, int gcd)
{
if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
System.out.println( "a = " + Math.min(gcd, sum - gcd)
+ ", b = " + ( int )(sum - Math.min(gcd, sum - gcd)) );
else
System.out.println( - 1 );
}
public static void main(String args[])
{
int sum = 8 ;
int gcd = 2 ;
findTwoNumbers(sum, gcd);
}
}
|
Python3
from math import gcd as __gcd
def findTwoNumbers( sum , gcd):
if (__gcd(gcd, sum - gcd) = = gcd and
sum ! = gcd):
print ( "a =" , min (gcd, sum - gcd),
", b =" , sum - min (gcd, sum - gcd))
else :
print ( - 1 )
if __name__ = = '__main__' :
sum = 8
gcd = 2
findTwoNumbers( sum , gcd)
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static void findTwoNumbers( int sum, int gcd)
{
if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
Console.WriteLine( "a = " + Math.Min(gcd, sum - gcd) +
", b = " + ( int )(sum - Math.Min(gcd, sum - gcd)));
else
Console.WriteLine( -1 );
}
public static void Main()
{
int sum = 8;
int gcd = 2;
findTwoNumbers(sum, gcd);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function findTwoNumbers( $sum , $gcd )
{
if (__gcd( $gcd , $sum - $gcd ) == $gcd &&
$sum != $gcd )
echo "a = " , min( $gcd , $sum - $gcd ),
" b = " , (int)( $sum - min( $gcd ,
$sum - $gcd ));
else
echo (-1);
}
$sum = 8;
$gcd = 2;
findTwoNumbers( $sum , $gcd );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (b==0) return a;
return __gcd(b,a%b);
}
function findTwoNumbers(sum, gcd)
{
if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
document.write( "a = " + Math.min(gcd, sum - gcd)
+ ", b = " + (sum - Math.min(gcd, sum - gcd)) );
else
document.write( -1 );
}
let sum = 8;
let gcd = 2;
findTwoNumbers(sum, gcd);
</script>
|
Time Complexity: O(log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...