- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

## Problem statement

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

## Example

If A = 2 and B = 4 then answer will be 2.

## Algorithm

- 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)

## Example

#include <iostream> using namespace std; int getGcd(int a, int b) { if (a == 0) { return b; } return getGcd(b % a, a); } int main() { cout << "Answer = " << getGcd(2, 4) << endl; return 0; }

## Output

When you compile and execute above program. It generates following output −

Answer = 2

- Related Questions & Answers
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Construct SLR (1) parsing table for the following grammar S → x A y |x B y |x A z A → q s | q B → q
- Count of all possible values of X such that A % X = B in C++
- Minimum positive integer divisible by C and is not in range [A, B] in C++
- Find multiple of x closest to or a ^ b (a raised to power b) in C++
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Find FIRST & FOLLOW for the following Grammar. S → A a A | B b B A → b B B → ε
- Maximum positive integer divisible by C and is in the range [A, B] in C++
- Find the multiple of x which is closest to a^b in C++
- Larger of a^b or b^a in C++
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Construct a Finite Automata for the regular expression ((a+b)(a+b))*.
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Find Positive Integer Solution for a Given Equation in C++
- Python a += b is not always a = a + b

Advertisements