Case Work
Case work problems are those where the solution is found by splitting the problem into different cases, solving each one separately, and then combining the results.
What is case work?
In many problems you might try to write one formula or one loop to cover everything. In a case work problem, you notice that different situations behave differently. So instead of one generic rule, you say:
- If case A happens, do this.
- If case B happens, do that.
- Maybe also case C, case D, etc.
Then you either add up the answers of all the cases, or pick the best one.
Why use case work?
- It allows you to handle special situations cleanly (when simple logic alone doesn’t work).
- It helps you break down a tricky problem into manageable pieces.
- It often appears in counting problems or problems with multiple outcomes.
How to do case work
- Read the problem carefully and look for places where things change (for example, small/large, positive/negative, even/odd).
- Identify all distinct cases that cover all possibilities without overlap.
- For each case, figure out how to compute the result.
- Combine the results if needed (add them or take min/max).
- Double-check you didn’t miss any case or count something twice.
Example problems
Even or odd sum
You have two numbers \(a\) and \(b\). Count how many pairs \((i, j)\) with \(1 \leq i \leq a\) and \(1 \leq j \leq b\) have \(i + j\) even.
Solution
Observation: \(i + j\) is even if both are even or both are odd.
So there are two cases:
- Case 1: \(i\) even and \(j\) even
- Case 2: \(i\) odd and \(j\) odd
Compute each count and add them together.
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int evenA = a / 2;
int oddA = a - evenA;
int evenB = b / 2;
int oddB = b - evenB;
int pairs = evenA * evenB + oddA * oddB;
cout << pairs;
}Triangle validity
Given three side lengths \(a, b, c\), determine if they can form a triangle.
Solution
Three positive integers can be sides of some triangle if the sum of any two is always greater than the third integer.
- \(b + c > a\)
- \(c + a > b\)
- \(a + b > c\)
If all of these are inequalities are true, a triangle is possible. We will be checking each of these inequalities, and in case any of them is not satisfied, the answer is “no”, otherwise, the answer is “yes”.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
bool ok = true;
if (b + c <= a) ok = false;
if (a + c <= b) ok = false;
if (a + b <= c) ok = false;
cout << (ok ? "YES" : "NO");
}Coordinate quadrant
Given \((x, y)\), print which quadrant the point belongs to.
Solution
Cases:
- \(x > 0, y > 0\) → Quadrant I
- \(x < 0, y > 0\) → Quadrant II
- \(x < 0, y < 0\) → Quadrant III
- \(x > 0, y < 0\) → Quadrant IV
- If \(x = 0\) or \(y = 0\) → on an axis
#include <iostream>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
if (x > 0 && y > 0) cout << "Quadrant I";
else if (x < 0 && y > 0) cout << "Quadrant II";
else if (x < 0 && y < 0) cout << "Quadrant III";
else if (x > 0 && y < 0) cout << "Quadrant IV";
else cout << "On an axis";
}Tips for case work
- Make sure your cases don’t overlap.
- Ensure all possibilities are covered.
- Watch for boundaries (like 0 or equality).
- Keep the logic clear and simple.
Quick summary
- Case work means breaking a problem into smaller, simpler situations.
- Solve each case separately and combine results.
- It’s a thinking technique, not a special algorithm.
- Great for early problem-solving practice.
Tip: When unsure how to start, think “What are the possible situations?” — that’s the beginning of case work.