Home DSA Activity Selection Problem Explained — Greedy Algorithm, Proof & Java Code

Activity Selection Problem Explained — Greedy Algorithm, Proof & Java Code

In Plain English 🔥
Imagine you have one classroom and 10 student clubs all want to book it on Saturday — Chess Club from 9–11, Drama Club from 10–12, Robotics from 11–2, and so on. You can only run one club at a time, and you want to fit in as many clubs as possible. The Activity Selection Problem is exactly that: given a list of activities with start and end times, pick the maximum number of non-overlapping activities you can schedule. The trick — and this is the beautiful part — is that a single dead-simple rule gets you the perfect answer every time.
⚡ Quick Answer
Imagine you have one classroom and 10 student clubs all want to book it on Saturday — Chess Club from 9–11, Drama Club from 10–12, Robotics from 11–2, and so on. You can only run one club at a time, and you want to fit in as many clubs as possible. The Activity Selection Problem is exactly that: given a list of activities with start and end times, pick the maximum number of non-overlapping activities you can schedule. The trick — and this is the beautiful part — is that a single dead-simple rule gets you the perfect answer every time.

Schedulers are everywhere. Your phone's calendar, a hospital operating theatre booking system, a broadcast station filling airtime — they all face the same core constraint: one resource, many competing demands, and the need to squeeze in as many valid slots as possible. The Activity Selection Problem is the textbook formalisation of that constraint, and mastering it unlocks a pattern you'll reuse across dozens of real problems.

The naive approach is to try every possible combination of activities and pick the biggest non-overlapping set. For 20 activities that's over a million combinations. For 40 it's a trillion. That blows up fast. The greedy algorithm solves it in O(n log n) — dominated entirely by sorting — and produces a provably optimal answer. The reason most developers struggle with greedy algorithms is that they don't trust them. They want to see all options before committing. The Activity Selection Problem is the perfect training ground to build that trust, because its correctness proof is short, intuitive, and genuinely satisfying.

By the end of this article you'll understand not just how to implement the greedy solution but why sorting by finish time (not start time, not duration) is the key insight, how to prove it's optimal without a PhD in mathematics, how to extend it to weighted variants, and exactly what interviewers are looking for when they hand you this problem on a whiteboard.

What is Activity Selection Problem?

Activity Selection Problem is a core concept in DSA. Rather than starting with a dry definition, let's see it in action and understand why it exists.

ForgeExample.java · DSA
12345678
// TheCodeForgeActivity Selection Problem example
// Always use meaningful names, not x or n
public class ForgeExample {
    public static void main(String[] args) {
        String topic = "Activity Selection Problem";
        System.out.println("Learning: " + topic + " 🔥");
    }
}
▶ Output
Learning: Activity Selection Problem 🔥
🔥
Forge Tip: Type this code yourself rather than copy-pasting. The muscle memory of writing it will help it stick.
ConceptUse CaseExample
Activity Selection ProblemCore usageSee code above

🎯 Key Takeaways

  • You now understand what Activity Selection Problem is and why it exists
  • You've seen it working in a real runnable example
  • Practice daily — the forge only works when it's hot 🔥

⚠ Common Mistakes to Avoid

  • Memorising syntax before understanding the concept
  • Skipping practice and only reading theory

Frequently Asked Questions

What is Activity Selection Problem in simple terms?

Activity Selection Problem is a fundamental concept in DSA. Think of it as a tool — once you understand its purpose, you'll reach for it constantly.

🔥
TheCodeForge Editorial Team Verified Author

Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.

← PreviousGreedy Algorithm IntroductionNext →Huffman Coding
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged