Program to check whether we can eat favorite candy on our favorite day in Python

Suppose we have an array of positive values candiesCount where candiesCount[i] denotes the number of candies of the ith type we have. We also have another array called queries where queries[i] has three parameters [favoriteType_i, favoriteDay_i, dailyCap_i]. We have some rules:

  • We start eating candies on day 0.

  • We cannot eat any candy of type i unless we have eaten all candies of previous i-1 types.

  • We must eat at least one candy per day until we have eaten all of them.

Following these rules, we have to make an array of Boolean values for each query result. The i-th entry is true if we can eat a candy of type favoriteType_i on day favoriteDay_i without eating more than dailyCap_i candies on any day.

Example Explanation

If the input is candiesCount = [7,4,5,3,8] and queries = [[0,2,2],[4,2,4],[2,13,100]], then the output will be [True, False, True] because ?

  • Query 1: If we eat 2 type-0 candies on day 0 and day 1, we can eat a type-0 candy on day 2.

  • Query 2: We can eat at most 4 candies each day. If we eat 4 candies every day, we will eat 4 type-0 candies on day 0 and some type-1 candies on day 1. By day 2, we cannot reach type-4 candies yet.

  • Query 3: If we eat 1 candy each day, we will eventually eat a type-2 candy on day 13.

Algorithm

To solve this problem, we will follow these steps ?

  • Create a cumulative sum array to track total candies up to each type

  • For each query, check if we can reach the favorite candy type by the favorite day

  • Verify that we don't exceed the daily capacity limit

Implementation

def solve(candiesCount, queries):
    # Create cumulative sum array
    sumcandy = [candiesCount[0]]
    index = 1
    while index < len(candiesCount):
        sumcandy.append(sumcandy[index-1] + candiesCount[index])
        index += 1
    sumcandy.append(0)  # Add 0 for boundary condition
    
    result = []
    for each in queries:
        candy_type = each[0]
        day = each[1]
        daily_cap = each[2]
        
        # Check if we can eat the favorite candy on the favorite day
        # Two conditions must be met:
        # 1. We have enough days to reach this candy type (eating at least 1 per day)
        # 2. We don't finish this candy type too early (respecting daily cap)
        if day + 1 > sumcandy[candy_type] or (day + 1) * daily_cap <= sumcandy[candy_type - 1]:
            result.append(False)
        else:
            result.append(True)
    
    return result

# Test with the given example
candiesCount = [7, 4, 5, 3, 8]
queries = [[0, 2, 2], [4, 2, 4], [2, 13, 100]]
print(solve(candiesCount, queries))
[True, False, True]

How It Works

The algorithm creates a cumulative sum array where sumcandy[i] represents the total number of candies from type 0 to type i. For each query, we check two conditions ?

  • Minimum time check: day + 1 > sumcandy[candy_type] - We need at least sumcandy[candy_type] days to finish all candies up to the favorite type (eating 1 per day minimum)

  • Maximum capacity check: (day + 1) * daily_cap - If we eat at maximum capacity every day, we shouldn't finish all previous candy types before reaching the favorite day

Conclusion

This solution efficiently determines if we can eat a favorite candy on a specific day by using cumulative sums and checking timing constraints. The algorithm runs in O(n + m) time where n is the number of candy types and m is the number of queries.

Updated on: 2026-03-26T14:02:44+05:30

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements