Given three integers a, b and c, determine whether there exist integers x and y that satisfy the Linear Diophantine equation.
Note: A Linear Diophantine Equation is of the form ax + by = c, where integer solutions for x and y are checked.
Examples:
Input: a = 3, b = 6, c = 9
Output: true
Explanation: The Equation is 3x + 6y = 9, one integral solution would be x = 1 , y = 1
Input: a = 3, b = 6, c = 8
Output: false
Explanation: No integral values of x and y exists that can satisfy the equation 3x + 6y = 8
Input: a = 2, b = 5, c = 1
Output: true
Explanation: Various integral solutions possible are, (-2,1) , (3,-1) etc.
Diophantine equation has a solution if and only if the greatest common divisor of a and b divides c. Let g = gcd(a, b), then if c % g == 0, a solution exists otherwise no solution exists.
Why does this solution work?
Let GCD of 'a' and 'b' be 'g'. g divides a and b. This implies g also divides (ax + by) (if x and y are integers). This implies gcd also divides 'c' using the relation that ax + by = c.
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
bool isPossible(int a, int b, int c) {
int g = gcd(a, b);
// if c is divisible by g then the solution is possible
return (c % g == 0);
}
int main() {
int a = 3, b = 6, c = 9;
cout << (isPossible(a, b, c) ? "true" : "false") << endl;
a = 3; b = 6; c = 8;
cout << (isPossible(a, b, c) ? "true" : "false") << endl;
a = 2; b = 5; c = 1;
cout << (isPossible(a, b, c) ? "true" : "false") << endl;
return 0;
}
import java.util.*;
class GFG {
public static int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static boolean isPossible(int a, int b, int c) {
int g = gcd(a, b);
// if c is divisible by g then the solution is possible
return (c % g == 0);
}
public static void main(String[] args) {
int a = 3, b = 6, c = 9;
System.out.println(isPossible(a, b, c) ? "true" : "false");
a = 3; b = 6; c = 8;
System.out.println(isPossible(a, b, c) ? "true" : "false");
a = 2; b = 5; c = 1;
System.out.println(isPossible(a, b, c) ? "true" : "false");
}
}
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def isPossible(a, b, c):
g = gcd(a, b)
# if c is divisible by g then the solution is possible
return c % g == 0
if __name__ == "__main__":
a, b, c = 3, 6, 9
print("true" if isPossible(a, b, c) else "false")
a, b, c = 3, 6, 8
print("true" if isPossible(a, b, c) else "false")
a, b, c = 2, 5, 1
print("true" if isPossible(a, b, c) else "false")
using System;
class GFG {
static int GCD(int a, int b) {
if (a == 0) return b;
return GCD(b % a, a);
}
static bool IsPossible(int a, int b, int c) {
int g = GCD(a, b);
// if c is divisible by g then the solution is possible
return (c % g == 0);
}
static void Main(string[] args) {
int a = 3, b = 6, c = 9;
Console.WriteLine(IsPossible(a, b, c) ? "true" : "false");
a = 3; b = 6; c = 8;
Console.WriteLine(IsPossible(a, b, c) ? "true" : "false");
a = 2; b = 5; c = 1;
Console.WriteLine(IsPossible(a, b, c) ? "true" : "false");
}
}
function gcd(a, b) {
if (a === 0) return b;
return gcd(b % a, a);
}
function isPossible(a, b, c) {
const g = gcd(a, b);
// if c is divisible by g then the solution is possible
return c % g === 0;
}
//Driver Code
let a = 3, b = 6, c = 9;
console.log(isPossible(a, b, c) ? "true" : "false");
a = 3; b = 6; c = 8;
console.log(isPossible(a, b, c) ? "true" : "false");
a = 2; b = 5; c = 1;
console.log(isPossible(a, b, c) ? "true" : "false");
Output
true false true
Time Complexity: The program’s complexity is dominated by the GCD computation, which uses the Euclidean algorithm. The Euclidean algorithm runs in O(log min(a, b)) time. Since the isPossible function calls gcd only once, its time complexity is also O(log min(a, b)).
Auxiliary Space : The program uses a fixed number of variables (a, b, c, gcd, and a few booleans/strings). These do not depend on input size, so the auxiliary space is O(1) (constant).