Eat Pizzas! - Problem
You are given an integer array pizzas of size n, where pizzas[i] represents the weight of the i-th pizza.
Every day, you eat exactly 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights W, X, Y, and Z, where W ≤ X ≤ Y ≤ Z, you gain the weight of only 1 pizza!
On odd-numbered days (1-indexed), you gain a weight of Z (the heaviest pizza).
On even-numbered days, you gain a weight of Y (the second heaviest pizza).
Find the maximum total weight you can gain by eating all pizzas optimally.
Note: It is guaranteed that n is a multiple of 4, and each pizza can be eaten only once.
Input & Output
Example 1 — Single Day
$
Input:
pizzas = [3, 1, 4, 2]
›
Output:
4
💡 Note:
We have 4 pizzas, so exactly 1 day of eating. Day 1 is odd, so we gain the weight of the heaviest pizza. Sorting [3,1,4,2] gives [1,2,3,4], so we gain weight 4.
Example 2 — Multiple Days
$
Input:
pizzas = [10, 8, 7, 6, 5, 4, 3, 2]
›
Output:
25
💡 Note:
We have 8 pizzas = 2 days. Day 1 (odd): gain heaviest from group = 10. Day 2 (even): gain second heaviest from group = 8. Optimal grouping gives us 10 + 8 + 7 = 25.
Example 3 — Equal Weights
$
Input:
pizzas = [5, 5, 5, 5]
›
Output:
5
💡 Note:
All pizzas have equal weight. Day 1 (odd): any grouping will have Z = 5, so we gain 5. Total weight gained is 5.
Constraints
- 4 ≤ pizzas.length ≤ 1000
- pizzas.length is a multiple of 4
- 1 ≤ pizzas[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of pizza weights, must eat 4 per day
2
Strategy
Group pizzas and assign to odd/even days optimally
3
Result
Maximum total weight gained
Key Takeaway
🎯 Key Insight: Sort pizzas and strategically assign the heaviest ones to positions that contribute to weight gain (Z on odd days, Y on even days)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code