Can You Eat Your Favorite Candy on Your Favorite Day? - Problem

You are given a 0-indexed array of positive integers candiesCount where candiesCount[i] represents the number of candies of the i-th type you have. You are also given a 2D array queries where queries[i] = [favoriteType_i, favoriteDay_i, dailyCap_i].

You play a game with the following rules:

  • You start eating candies on day 0.
  • You cannot eat any candy of type i unless you have eaten all candies of type i - 1.
  • You must eat at least one candy per day until you have eaten all the candies.

For each query, determine if you can eat a candy of type favoriteType_i on day favoriteDay_i without eating more than dailyCap_i candies on any day.

Return a boolean array answer where answer[i] is true if you can eat your favorite candy on your favorite day for query i, and false otherwise.

Input & Output

Example 1 — Multiple Query Types
$ Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
Output: [true,false,true]
💡 Note: Query 1: Type 0 candy available days 0-6, day 2 ∈ [0,6] ✓. Query 2: Type 4 needs 11 days minimum, but day 2 < 11 ✗. Query 3: Type 2 available days 1-15, day 13 ∈ [1,15] ✓
Example 2 — Single Type Query
$ Input: candiesCount = [5,2,6,4,1], queries = [[3,1,6],[4,10,3],[3,0,1]]
Output: [false,true,false]
💡 Note: Query 1: Need 7 days minimum for type 3, day 1 too early. Query 2: Type 4 available days 3-17, day 10 ∈ [3,17] ✓. Query 3: Type 3 earliest day 7, day 0 too early
Example 3 — Edge Case Small Array
$ Input: candiesCount = [1], queries = [[0,0,1],[0,1,1]]
Output: [true,false]
💡 Note: Only 1 candy of type 0. Available only on day 0. First query matches exactly, second query too late

Constraints

  • 1 ≤ candiesCount.length ≤ 105
  • 1 ≤ candiesCount[i] ≤ 105
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 3
  • 0 ≤ favoriteType_i < candiesCount.length
  • 0 ≤ favoriteDay_i ≤ 109
  • 1 ≤ dailyCap_i ≤ 109

Visualization

Tap to expand
Candy Eating Problem - Prefix Sum Solution INPUT candiesCount Array: 7 i=0 4 i=1 5 i=2 3 i=3 8 i=4 Queries: Q0: type=0, day=2, cap=2 Can eat type 0 on day 2? Q1: type=4, day=2, cap=4 Can eat type 4 on day 2? Q2: type=2, day=13, cap=1B Can eat type 2 on day 13? Prefix: [0,7,11,16,19,27] Total candies before each type ALGORITHM STEPS 1 Build Prefix Sum prefix[i] = total candies before type i 2 Calculate Range minEat = day + 1 (eat 1/day) maxEat = (day+1) * cap 3 Get Type Bounds typeStart = prefix[type]+1 typeEnd = prefix[type+1] 4 Check Overlap Ranges must overlap: [minEat,maxEat] with [typeStart,typeEnd] Example Q0 (type=0,day=2,cap=2): minEat=3, maxEat=6 typeStart=1, typeEnd=7 [3,6] overlaps [1,7] Result: TRUE FINAL RESULT Query Results: Query 0: TRUE Day range [3,6] reaches type 0 candy range [1,7]. Overlap exists! Query 1: FALSE Day range [3,12] cannot reach type 4 candy range [20,27] Query 2: TRUE Day range [14,14B] reaches type 2 candy range [12,16]. Overlap exists! OUTPUT [true, false, true] Key Insight: Use prefix sums to track cumulative candy counts. For each query, calculate the range of candies you can eat by the target day [day+1, (day+1)*cap] and check if it overlaps with the range of candies for the target type [prefix[type]+1, prefix[type+1]]. Time: O(n+q), Space: O(n) TutorialsPoint - Can You Eat Your Favorite Candy on Your Favorite Day? | Optimal Prefix Sum Approach
Asked in
Google 15 Amazon 8 Facebook 6
38.5K 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