# Minimum positive integer value possible of X for given A and B in X = P*A + Q*B

Given values of A and B, find the minimum positive integer value of X that can be achieved in the equation X = P*A + P*B, Here P and Q can be zero or any positive or negative integer.

Examples:

Input : A = 3 B = 2 Output : 1 Input : A = 2 B = 4 Output : 2

Basically we need to find P and Q such that P*A > P*B and P*A – P*B is minimum positive integer. This problem can be easily solved by calculating GCD of both numbers.

For example:

For A = 2 And B = 4 Let P = 1 And Q = 0 X = P*A + Q*B = 1*2 + 0*4 = 2 + 0 = 2 (i. e GCD of 2 and 4) For A = 3 and B = 2 let P = -1 And Q = 2 X = P*A + Q*B = -1*3 + 2*2 = -3 + 4 = 1 ( i.e GCD of 2 and 3 )

Below is the implementation of above idea .

## CPP

`// CPP Program to find ` `// minimum value of X ` `// in equation X = P*A + Q*B ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to calculate GCD ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `(b % a, a); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `a = 2; ` ` ` `int` `b = 4; ` ` ` `cout << gcd(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find ` `// minimum value of X ` `// in equation X = P*A + Q*B ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG { ` ` ` `// utility function to calculate gcd ` ` ` ` ` `public` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `2` `; ` ` ` `int` `b = ` `4` `; ` ` ` ` ` `System.out.println(gcd(a, b)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find ` `# minimum value of X ` `# in equation X = P * A + Q * B ` ` ` `# Function to return gcd of a and b ` `def` `gcd(a, b): ` ` ` `if` `a ` `=` `=` `0` `: ` ` ` `return` `b ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `a ` `=` `2` `b ` `=` `4` `print` `(gcd(a, b)) ` |

*chevron_right*

*filter_none*

## C#

`// CSHARP Program to find ` `// minimum value of X ` `// in equation X = P*A + Q*B ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// function to get gcd of a and b ` ` ` `public` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 2; ` ` ` `int` `b = 4; ` ` ` `Console.WriteLine(gcd(a, b)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`// PHP Program to find ` `// minimum value of X ` `// in equation X = P*A + Q*B ` ` ` `<?php ` ` ` `// Function to return ` `// gcd of a and b ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// Driver Code ` `$a` `= 2; ` `$b` `= 4; ` `echo` `gcd(` `$a` `, ` `$b` `); ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum positive integer divisible by C and is not in range [A, B]
- Minimum positive integer to divide a number such that the result is an odd
- Print first k digits of 1/n where n is a positive integer
- Maximum positive integer divisible by C and is in the range [A, B]
- Count pairs in Array whose product is a Kth power of any positive integer
- Minimum decrements to make integer A divisible by integer B
- Count of index pairs in array whose range product is a positive integer
- Minimum Possible sum of digits in a positive multiple of N
- Minimum number of changes such that elements are first Negative and then Positive
- Count the minimum steps to reach 0 from the given integer N
- Minimum numbers needed to express every integer below N as a sum
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Ways to write N as sum of two or more positive integers | Set-2
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Find K distinct positive odd integers with sum N
- Number of subsequences with positive product
- Number of Subarrays with positive product

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.