Given two positive integers N and M., The task is to find the smallest divisor D of N such that gcd(D, M) > 1. If there are no such divisors, then print -1.
Input: N = 8, M = 10
Input: N = 8, M = 1
A naive approach is to iterate for every factor and calculate the gcd of the factor and M. If it exceeds M, then we have the answer.
Time Complexity: O(N * log max(N, M))
An efficient approach is to iterate till sqrt(n) and check for gcd(i, m). If gcd(i, m) > 1, then we print and break it, else we check for gcd(n/i, m) and store the minimal of them.
Below is the implementation of the above approach.
Time Complexity: O(sqrt N * log max(N, M))
- Smallest subarray of size greater than K with sum greater than a given value
- Highest and Smallest power of K less than and greater than equal to N respectively
- Smallest subarray such that all elements are greater than K
- Smallest integer greater than n such that it consists of digit m exactly k times
- Find the k-th smallest divisor of a natural number N
- Smallest prime divisor of a number
- Number formed after K times repeated addition of smallest divisor of N
- Smallest N digit number with none of its digits as its divisor
- Largest Divisor for each element in an array other than 1 and the number itself
- Smallest number greater than or equal to N divisible by K
- Smallest Greater (than S) String of length K whose letters are subset of S
- Smallest Special Prime which is greater than or equal to a given number
- Smallest power of 4 greater than or equal to N
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than Y with sum of digits equal to X
- Smallest subarray from a given Array with sum greater than or equal to K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number greater than n that can be represented as a sum of distinct power of k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.