Make It One
InterviewTime limit0.15sMemory limit128 MB
Compute the minimum number of divide-by-3, divide-by-2, or subtract-1 operations needed to reduce N to 1.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
For an integer X, you may use the following three operations.
- If X is divisible by 3, divide X by 3.
- If X is divisible by 2, divide X by 2.
- Subtract 1 from X.
Given an integer N, use these operations in any order to make N equal to 1. Find the minimum possible number of operations.
Input
The first line contains an integer N. (1 <= N <= 1,000,000)
Output
Print the minimum number of operations needed.
Hint
For N = 10, one optimal sequence is 10 -> 9 -> 3 -> 1, which uses 3 operations.