


1) Subtract 1 from it. (n = n - 1) ,
2) If n is divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) ,
3) If n is divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ).
Given:
‘N’ = 4, it will take 2 steps to reduce it to 1, i.e., first divide it by 2 giving 2 and then subtract 1, giving 1.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The following ‘T’ lines contain a single integer ‘N’, denoting the number given to us.
For each test case, You are supposed to return an integer that denotes the minimum steps it takes to reduce the number to 1.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= ‘T’ <= 5
1 <= ‘N’ <= 10 ^ 5
Time Limit: 1sec.
The idea is to use recursion to find all possible cases and then, out of them, choose the minimum.
The steps are as follows:
The idea is to use recursion to find all possible cases and then, out of them, choose the minimum. As there are maximum ‘N’ states, we can use a ‘dp’ array to store the recurring sub-problems, where ‘dp[N]’ represents the minimum number of steps it takes to convert ‘N’ to 1.
The steps are as follows: