Problem

You are given an integer array pizzas of size n, where pizzas[i] represents the weight of the ith 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.
  • On even-numbered days, you gain a weight of Y.

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.

Example 1

1
2
3
4
5
6
Input: pizzas = [1,2,3,4,5,6,7,8]
Output: 14
Explanation:
* On day 1, you eat pizzas at indices `[1, 2, 4, 7] = [2, 3, 5, 8]`. You gain a weight of 8.
* On day 2, you eat pizzas at indices `[0, 3, 5, 6] = [1, 4, 6, 7]`. You gain a weight of 6.
The total weight gained after eating all the pizzas is `8 + 6 = 14`.

Example 2

1
2
3
4
5
6
Input: pizzas = [2,1,1,1,1,1,1,1]
Output: 3
Explanation:
* On day 1, you eat pizzas at indices `[4, 5, 6, 0] = [1, 1, 1, 2]`. You gain a weight of 2.
* On day 2, you eat pizzas at indices `[1, 2, 3, 7] = [1, 1, 1, 1]`. You gain a weight of 1.
The total weight gained after eating all the pizzas is `2 + 1 = 3.`

Constraints

  • 4 <= n == pizzas.length <= 2 * 10^5
  • 1 <= pizzas[i] <= 10^5
  • n is a multiple of 4.

Examples

Solution

Method 1 – Greedy Pairing

Intuition

To maximize the total weight gained, we want to maximize the sum of the largest pizza in each group on odd days and the second largest on even days. By sorting the pizzas and grouping them optimally, we can always pick the largest available for odd days and the second largest for even days.

Approach

  1. Sort the pizzas in non-decreasing order.
  2. Divide the pizzas into groups of 4 (since n is a multiple of 4).
  3. For each group, on odd days, pick the largest (Z), and on even days, pick the second largest (Y).
  4. Alternate between odd and even days, summing the appropriate pizza weights.
  5. Return the total sum.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    int eatPizzas(vector<int>& pizzas) {
        sort(pizzas.begin(), pizzas.end());
        int n = pizzas.size(), ans = 0;
        int days = n / 4;
        for (int i = 0; i < days; ++i) {
            int base = i * 4;
            if (i % 2 == 0) ans += pizzas[base + 3]; // odd day: Z
            else ans += pizzas[base + 2]; // even day: Y
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func eatPizzas(pizzas []int) int {
    sort.Ints(pizzas)
    n := len(pizzas)
    ans := 0
    days := n / 4
    for i := 0; i < days; i++ {
        base := i * 4
        if i%2 == 0 {
            ans += pizzas[base+3]
        } else {
            ans += pizzas[base+2]
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public int eatPizzas(int[] pizzas) {
        Arrays.sort(pizzas);
        int n = pizzas.length, ans = 0, days = n / 4;
        for (int i = 0; i < days; ++i) {
            int base = i * 4;
            if (i % 2 == 0) ans += pizzas[base + 3];
            else ans += pizzas[base + 2];
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    fun eatPizzas(pizzas: IntArray): Int {
        pizzas.sort()
        val n = pizzas.size
        var ans = 0
        val days = n / 4
        for (i in 0 until days) {
            val base = i * 4
            ans += if (i % 2 == 0) pizzas[base + 3] else pizzas[base + 2]
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def eatPizzas(self, pizzas: list[int]) -> int:
        pizzas.sort()
        n = len(pizzas)
        ans = 0
        days = n // 4
        for i in range(days):
            base = i * 4
            if i % 2 == 0:
                ans += pizzas[base + 3]
            else:
                ans += pizzas[base + 2]
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
impl Solution {
    pub fn eat_pizzas(mut pizzas: Vec<i32>) -> i32 {
        pizzas.sort();
        let n = pizzas.len();
        let mut ans = 0;
        let days = n / 4;
        for i in 0..days {
            let base = i * 4;
            if i % 2 == 0 {
                ans += pizzas[base + 3];
            } else {
                ans += pizzas[base + 2];
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    eatPizzas(pizzas: number[]): number {
        pizzas.sort((a, b) => a - b);
        const n = pizzas.length;
        let ans = 0;
        const days = n / 4;
        for (let i = 0; i < days; ++i) {
            const base = i * 4;
            ans += i % 2 === 0 ? pizzas[base + 3] : pizzas[base + 2];
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n log n), for sorting the pizzas.
  • 🧺 Space complexity: O(1), ignoring the sort’s space if in-place.