![]() |
VOOZH | about |
Given an integer N, the task is to calculate the maximum number of times N can be divided by an integer K, where K is a power of a prime integer and the value of K is always distinct.
Example:
Input: N = 24
Output: 3
Explanation: In the first operation, K = 2 (=21). Hence, the value of N becomes N = 24/2 = 12. In the second operation, K = 3. Hence, N = 12/3 = 4. In the 3rd operation, K = 4 (=22). Hence, N = 4/4 = 1, which can not be further divided. Therefore, {2, 3, 4} is the largest set 3 integers having distinct powers of prime integers than can divide N.Input: N = 100
Output: 2
Approach: The given problem can be solved using a greedy approach. The idea is to divide the number by all of its prime factors in increasing order of their value as well as power. Iterate using a variable i in the range [2, ?N] and if i divides N, then divide N with increasing powers of i (i.e, 1, 2, 3...) until it is divisible with it. Maintain the count of the number of divisions in a variable which is the required answer.
Below is the implementation of the above approach:
2
Time Complexity: O(?N)
Auxiliary Space: O(1)