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.
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Reading a single integer
    int n;
    cin >> n;

    // Reading multiple values
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;

    // Reading n integers into a loop
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        cout << x * 2 << "\n";
    }

    return 0;
}
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:
TypeSizeRangeWhen to use
int4 bytes≈ ±2.1 × 10⁹Most problems
long long8 bytes≈ ±9.2 × 10¹⁸When n > 10⁹ or products/sums can overflow
double8 bytes~15 decimal digitsFloating point problems
char1 byte-128 to 127Single characters
bool1 bytetrue/falseFlags
Integer overflow is the #1 beginner mistake. If the problem says n ≤ 10⁹, then n * n overflows int. Use long long!

Basic math operations

// Division (integer)
int a = 7, b = 2;
cout << a / b;     // 3 (truncated)
cout << a % b;     // 1 (remainder)

// Min and max
cout << min(a, b); // 2
cout << max(a, b); // 7

// Absolute value
cout << abs(-5);   // 5

// Power (for integers, write your own)
// Don't use pow() for integers — it returns double and has precision issues
long long power(long long base, long long exp) {
    long long result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) result *= base;
        base *= base;
        exp /= 2;
    }
    return result;
}

Simple problem patterns

Pattern 1: Process and output

Read input, do some calculation, print the result.
// Example: Is a number even or odd?
int n;
cin >> n;
if (n % 2 == 0) cout << "Even";
else cout << "Odd";

Pattern 2: Count something

Count how many elements satisfy a condition.
int n, count = 0;
cin >> n;
for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0) count++;
}
cout << count;

Pattern 3: Find min/max

Track the minimum or maximum as you iterate.
int n;
cin >> n;
int mn = INT_MAX, mx = INT_MIN;
for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    mn = min(mn, x);
    mx = max(mx, x);
}
cout << mn << " " << mx;

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.