Google | Onsite | Min operations to reduce number to 1
15219

Given a positive integer n and 3 operations on n:

  1. n - 1
  2. n / 2 (if n is even)
  3. n / 3 (if n % 3 == 0)

Find the minimum number of above operations to reduce n to 1.

Example 1:

Input: n = 9
Output: 2
Explanation:
Step 1: 9 / 3 = 3
Step 2: 3 / 3 = 1

Example 2:

Input: n = 8
Output: 3
Explanation:
Step 1: 8 / 2 = 4
Step 2: 4 / 2 = 2
Step 3: 2 - 1 = 1

Example 3:

Input: n = 28
Output: 4
Comments (29)