Minimum Number of Days to Eat N Oranges

There are n oranges in the kitchen, and you decided to eat some of these oranges every day as follows:

  • Eat one orange.
  • If the number of remaining oranges n is divisible by 2, then you can eat n / 2 oranges.
  • If the number of remaining oranges n is divisible by 3, then you can eat 2 * (n / 3) oranges.

You can only choose one of the actions per day.

Given the integer n, return the minimum number of days to eat n oranges.

Example 1
Inputn = 10
Output4
Starting with 10 oranges, one optimal sequence eats 1 orange, then 6 oranges, then 2 oranges, then the last orange, for a minimum of 4 days.
Example 2
Inputn = 6
Output3
Starting with 6 oranges, one optimal sequence eats 3 oranges, then 2 oranges, then the last orange, for a minimum of 3 days.

Constraints

  • 1 <= n <= 2 * 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate