- 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

Given two integer p and q, the task is to find the minimum possible number x such that q % x = 0 and x % p = 0. If the conditions aren’t true for any number, then print -1.

If p = 3 and q = 66 then answer is 3 as: 66 % 3 = 0 3 % 3 = 0

- If a number x satisfies the given condition, then it’s obvious that q will be divided by p i.e. q % p = 0 because x is a multiple of p and q is a multiple of x
- So the minimum possible value of x will be the GCD of p and q and when q is not divisible by p then no number will satisfy the given condition

#include <bits/stdc++.h> using namespace std; int getMinValue(int p, int q) { if (q % p == 0) { return __gcd(p, q); } return -1; } int main() { int p = 3; int q = 66; cout << "Minimum value = " << getMinValue(p, q) << endl; return 0; }

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

Minimum value = 3

- Related Questions & Answers
- Print array elements that are divisible by at-least one other in C++
- Find a number that divides maximum array elements in C++
- Find maximum power of a number that divides a factorial in C++
- Count elements that are divisible by at-least one element in another array in C++
- Generating a random number that is divisible by n in JavaScript
- Smallest number that is divisible by first n numbers in JavaScript
- Finding a pair that is divisible by some number in JavaScript
- Find integers that divides maximum number of elements of the array in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum positive integer divisible by C and is not in range [A, B] in C++
- Maximum and Minimum element of a linked list which is divisible by a given number k in C++
- Find nth number that contains the digit k or divisible by k in C++
- Minimum number of coins that make a given value
- Minimum removals in a number to be divisible by 10 power raised to K in C++
- Find the number closest to n and divisible by m in C++

Advertisements