Given a number N, the task is to find the minimum value of N by applying below operations any number of times:
- Multiply N by any positive integer
- Replace N with sqrt(N), only if N is a perfect square.
Input: N = 20
Multiply -> 20 * 5 = 100
sqrt(100) = 10, which is the minimum value obtainable.
Input: N = 5184
sqrt(5184) = 72.
Multiply -> 72*18 = 1296
sqrt(1296) = 6, which is the minimum value obtainable.
Approach: This problem can be solved using Greedy Approach. Below are the steps:
- Keep replacing N to sqrt(N) until N is a perfect square.
- After the above step, iterate from sqrt(N) to 2, and for every, i keep replacing N with N / i if N is divisible by i2.
- The value of N after the above step will be the minimum possible value.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Check if a number is perfect square without finding square root
- Reduce a number to 1 by performing given operations | Set 2
- Reduce N to 1 with minimum number of given operations
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to reduce N to 0
- Count the number of operations required to reduce the given number
- Minimum number of given operations required to reduce the array to 0 element
- Square root of a number using log
- Square root of a number without using sqrt() function
- C program to find square root of a given number
- Square root of a number by Repeated Subtraction method
- Find square root of number upto given precision using binary search
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Find maximum operations to reduce N to 1
- Count operations of the given type required to reduce N to 0
- Square root of an integer
- Fast inverse square root
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.