Minimum Number of Days to Eat N Oranges - Problem

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.

Input & Output

Example 1 — Small Number
$ Input: n = 10
Output: 4
💡 Note: Day 1: eat 1 orange (9 remaining). Day 2: 9%3=0, eat 2*(9/3)=6 oranges (3 remaining). Day 3: 3%3=0, eat 2*(3/3)=2 oranges (1 remaining). Day 4: eat 1 orange (0 remaining). Total: 4 days.
Example 2 — Perfect Division
$ Input: n = 6
Output: 3
💡 Note: Day 1: 6%2=0, eat 6/2=3 oranges (3 remaining). Day 2: 3%3=0, eat 2*(3/3)=2 oranges (1 remaining). Day 3: eat 1 orange (0 remaining). Total: 3 days.
Example 3 — Base Case
$ Input: n = 1
Output: 1
💡 Note: Day 1: eat 1 orange (0 remaining). Total: 1 day.

Constraints

  • 1 ≤ n ≤ 2 × 109

Visualization

Tap to expand
Minimum Days to Eat N Oranges: Strategic Eating10Start: 10 orangesThree Options:1. Eat 1 orange2. If n%2=0: eat n/23. If n%3=0: eat 2n/3Optimal Path:Day 1: 10 → 9 (eat 1 orange)Day 2: 9 → 3 (eat 6 oranges, 9÷3)Day 3: 3 → 1 (eat 2 oranges, 3÷3)Day 4: 1 → 0 (eat 1 orange)4Minimum Days: 4Key: Use division shortcuts when possible!Focus on reaching numbers divisible by 2 or 3
Understanding the Visualization
1
Input
n=10 oranges to eat optimally
2
Strategy
Choose best eating option each day
3
Output
4 days minimum to eat all oranges
Key Takeaway
🎯 Key Insight: Always aim for numbers divisible by 2 or 3 to use the efficient batch eating options
Asked in
Google 45 Microsoft 32 Amazon 28 Facebook 21
42.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen