Minimum Pizzas

:memo: How to Solve This Problem in 1 Line with C++

In this problem, we are given two numbers, N and X, for each test case.
We need to calculate how many groups of size 4 are required if we have N*X items in total.


:small_blue_diamond: Key Idea

  • First, compute the total: total = N * X.
  • If total is divisible by 4, the answer is simply total / 4.
  • If not divisible by 4, we still need one extra group for the leftover items → answer becomes (total / 4) + 1.

This logic can be implemented in just one line using the ternary operator ?: in C++.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int T;
    cin >> T;
	
    for(int i = 0; i < T; i++) {
        int N, X;
        cin >> N >> X;
        
        // 1-line solution using ternary operator
        ((N*X)%4 == 0) ? cout << (N*X)/4 << endl : cout << ((N*X)/4) +1 << endl;
    }
}

Example:
Input

2
5 3
8 4

Output

4
8
  • For N=5, X=3 → total=15 → needs 4 groups.
  • For N=8, X=4 → total=32 → divides evenly, so 8 groups.

:white_check_mark: This way, the problem is solved in one line of code with the C++ ternary operator.