- 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

# Find smallest number K such that K % p = 0 and q % K = 0 in C++

Suppose we have two integers P and Q. We have to find smallest number K, such that K mod P = 0 and Q mod K = 0. Otherwise print -1. So if the P and Q are 2 and 8, then K will be 2. As 2 mod 2 = 0, and 8 mode 2 = 0.

In order for K to be possible, Q must be divisible by P. So if P mod Q = 0 then print P otherwise print -1.

## Example

#include<iostream> using namespace std; int getMinK(int p, int q) { if (q % p == 0) return p; return -1; } int main() { int p = 24, q = 48; cout << "Minimum value of K is: " << getMinK(p, q); }

## Output

Minimum value of K is: 24

- Related Questions & Answers
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Find nth number that contains the digit k or divisible by k in C++
- K and -K in Python
- Program to find smallest value of K for K-Similar Strings in Python
- Find smallest values of x and y such that ax – by = 0 in C++
- Print direction of moves such that you stay within the [-k, +k] boundary in C++
- Find K Pairs with Smallest Sums in C++
- Find K-th Smallest Pair Distance in C++
- Find smallest element greater than K in Python
- k-Rough Number or k-Jagged Number in C++

Advertisements