Given two positive integers P and Q, the task is to largest divisor of P which is not divisible by Q.
Examples:
Input: P = 10, Q = 4
Output: 10
Explanation: 10 is the largest number which divides 10 but not divisible by 4.Input: P = 12, Q = 6
Output: 4
Explanation: 4 is the largest number which divides 12 but not divisible by 6.
Approach: The simplest approach is to find all the divisors of P and obtain the greatest of these divisors, which is not divisible by Q.
Time Complexity: O(√P)
Auxiliary Space: O(1)
Alternate Approach: Follow the steps below to solve the problem:
- Store the count of frequencies of prime factors of Q in a HashMap divisors.
- Initialize a variable, say ans, to store the greatest number X, such that X divides P but is not divisible by Q.
- Iterate through all divisors of Q and perform the following steps:
- Store the count of frequencies of the current prime divisor in a variable, say frequency.
- Store the count of frequencies of the current prime divisor on dividing P in a variable, say cur, and initialize num as the current prime divisor * (frequency – cur + 1).
- If the value of cur is less than frequency, then update the variable ans to P and break out of the loop.
- Otherwise, divide P with num and store the result in a variable, say tempAns.
- After completing the above steps, update the value of ans to the maximum of ans and tempAns.
- After completing the above steps, print the value of ans as the maximum possible result.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the largest number // X such that it divides P but is not // divisible by Q void findTheGreatestX(int P, int Q) { // Stores the frequency count of // of all Prime Factors map<int, int> divisiors; for (int i = 2; i * i <= Q; i++) { while (Q % i == 0 and Q > 1) { Q /= i; // Increment the frequency // of the current prime factor divisiors[i]++; } } // If Q is a prime factor if (Q > 1) divisiors[Q]++; // Stores the desired result int ans = 0; // Iterate through all divisors of Q for (auto i : divisiors) { int frequency = i.second; int temp = P; // Stores the frequency count // of current prime divisor on // dividing P int cur = 0; while (temp % i.first == 0) { temp /= i.first; // Count the frequency of the // current prime factor cur++; } // If cur is less than frequency // then P is the final result if (cur < frequency) { ans = P; break; } temp = P; // Iterate to get temporary answer for (int j = cur; j >= frequency; j--) { temp /= i.first; } // Update current answer ans = max(temp, ans); } // Print the desired result cout << ans; } // Driver Code int main() { // Given P and Q int P = 10, Q = 4; // Function Call findTheGreatestX(P, Q); return 0; } |
10
Time Complexity: O(sqrt(Q) + log(P) * log(Q))
Auxiliary Space: O(Q)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Largest Divisor of a Number not divisible by a perfect square
- Largest Divisor for each element in an array other than 1 and the number itself
- Largest odd divisor Game to check which player wins
- Find permutation of n which is divisible by 3 but not divisible by 6
- Sum of greatest odd divisor of numbers in given range
- Find elements in a given range having at least one odd divisor
- Minimum divisor of a number to make the number perfect cube
- Greatest divisor which divides all natural number in range [L, R]
- 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
- Check if a number is divisible by all prime divisors of another number
- Max occurring divisor in an interval
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Generating numbers that are divisor of their right-rotations
- Find an integer X which is divisor of all except exactly one element in an array
- Maximum sum after repeatedly dividing N by a divisor
- Probability that an arbitrary positive divisor of 10^X is an integral multiple of 10^Y
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.

