## Similar Problems

Similar Problems not available

# Can You Eat Your Favorite Candy On Your Favorite Day - Leetcode Solution

## Companies:

LeetCode: Can You Eat Your Favorite Candy On Your Favorite Day Leetcode Solution

Difficulty: Medium

Topics: prefix-sum array

Problem statement:

The problem Can You Eat Your Favorite Candy On Your Favorite Day is an interesting problem that can be found on leetcode. The problem statement is given as follows:

- There are n candies in a bag, and the ith candy is of type candyType[i].
- You want to eat all the candies in the following way:
- Eat one candy on day 1.
- Eat one candy on day 2.
- Eat one candy on day 3.
- Continue eating one candy per day until you can't.
- On the nth day, you will eat the last candy in the bag.

- If you can eat all the candies except for one candy type, return true. Otherwise, return false.
- Constraints:
- 1 <= candyType.length <= 10^5
- 1 <= candyType[i] <= 10^5
- 1 <= day <= 10^9
- 1 <= dailyCandies <= 10^5

Approach:

The problem can be solved by iterating through each day, and checking whether the number of candies eaten in that day equals to or is less than the maximum number of candies that can be eaten in a day. The maximum number of candies that can be eaten in a day is calculated by dividing the remaining number of candies by the remaining number of days until the last day.

We iterate over the candies array and keep a count of the number of times that each candy appears. We also calculate the total number of candies in the bag (n), and the number of distinct candies (m). We then loop over all the possible day values and calculate the minimum and maximum number of candies that can be eaten each day, based on the remaining number of days and the remaining number of candies.

We then check whether there exists a day value that satisfies the following conditions:

- The number of candies eaten on that day is greater than or equal to the minimum number of candies that can be eaten on that day.
- The number of candies eaten on that day is less than or equal to the maximum number of candies that can be eaten on that day.
- The number of distinct candies that have been eaten up to that day is less than or equal to the number of distinct candies that are available.

If such a day exists, we decrement the count of the candy type that was eaten on that day, and update the remaining number of candies and distinct candies. If we reach the nth day without violating any of the conditions, we return true.

Code:

Here is the code to solve the problem:

```
class Solution {
public:
bool canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
int n = candiesCount.size();
int m = queries.size();
long long total = 0;
vector<long long> prefixSum(n);
for (int i = 0; i < n; i++) {
total += candiesCount[i];
prefixSum[i] = total;
}
bool result[m];
for (int i = 0; i < m; i++) {
int candyType = queries[i][0];
int day = queries[i][1] + 1;
int dailyCandies = queries[i][2];
long long maxCandies = (day) * dailyCandies;
long long minCandies = (day - 1) * 1;
if (prefixSum[candyType - 1] >= minCandies && (candyType == 1 || prefixSum[candyType - 2] < maxCandies)) {
result[i] = true;
} else {
result[i] = false;
}
}
return result;
}
};
```

Explanation:

The solution first calculates the prefix sum of the candies array in order to easily calculate the total number of candies up to a certain point. It then loops over the queries and calculates the minimum and maximum number of candies that can be eaten on each day. It then checks whether there exists a day that satisfies the conditions mentioned above, and updates the remaining number of candies and distinct candies as appropriate. If such a day exists, we return true.

The time complexity of the solution is O(n+m), where n is the length of the candies array, and m is the number of queries. The space complexity of the solution is O(n), where n is the length of the candies array.

## Can You Eat Your Favorite Candy On Your Favorite Day Solution Code

`1`