Skip to main content

What is problem solving?

Problem solving in competitive programming means reading a well-defined problem, understanding its constraints, designing an algorithm, and writing code that produces the correct output for all valid inputs — within time and memory limits.

The problem-solving process

Every problem follows the same workflow:
1

Read and understand

Read the problem statement carefully. Identify the input format, output format, and constraints. Re-read if anything is unclear.
2

Work through examples

Trace through the sample test cases by hand. Make sure you understand why the expected output is correct.
3

Design your approach

Think about the algorithm before writing code. Ask yourself: what data structure do I need? What’s the time complexity?
4

Write the code

Implement your solution. Keep it simple and clean.
5

Test and debug

Test with sample cases first, then think of edge cases (minimum input, maximum input, special cases).

Input and output in C++

The most basic skill — reading input and writing output efficiently.
Always use "\n" instead of endl — it’s significantly faster because endl flushes the output buffer.

Data types and their limits

Choosing the right data type prevents overflow bugs:
Integer overflow is the #1 beginner mistake. If the problem says n ≤ 10⁹, then n * n overflows int. Use long long!

Basic math operations

Simple problem patterns

Pattern 1: Process and output

Read input, do some calculation, print the result.

Pattern 2: Count something

Count how many elements satisfy a condition.

Pattern 3: Find min/max

Track the minimum or maximum as you iterate.

Practice

Head over to Sheet 1 — Intro to PS to practice these concepts with curated problems.

Sheet 1: Intro to PS

Practice problems for this tutorial.