> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hnuicpc.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Intro to problem solving

> Tutorial for Sheet 1 — Learn the fundamentals of competitive programming and problem solving.

<iframe width="100%" height="400" src="https://www.youtube.com/embed/yQSa6fdtZMM" title="Level 0 Session 1 Recording" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />

## 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:

<Steps>
  <Step title="Read and understand">
    Read the problem statement **carefully**. Identify the input format, output format, and constraints. Re-read if anything is unclear.
  </Step>

  <Step title="Work through examples">
    Trace through the sample test cases by hand. Make sure you understand **why** the expected output is correct.
  </Step>

  <Step title="Design your approach">
    Think about the algorithm before writing code. Ask yourself: what data structure do I need? What's the time complexity?
  </Step>

  <Step title="Write the code">
    Implement your solution. Keep it simple and clean.
  </Step>

  <Step title="Test and debug">
    Test with sample cases first, then think of edge cases (minimum input, maximum input, special cases).
  </Step>
</Steps>

## Input and output in C++

The most basic skill — reading input and writing output efficiently.

```cpp theme={null}
#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;
}
```

<Tip>
  Always use `"\n"` instead of `endl` — it's significantly faster because `endl` flushes the output buffer.
</Tip>

## Data types and their limits

Choosing the right data type prevents overflow bugs:

| Type        | Size    | Range               | When to use                                |
| ----------- | ------- | ------------------- | ------------------------------------------ |
| `int`       | 4 bytes | ≈ ±2.1 × 10⁹        | Most problems                              |
| `long long` | 8 bytes | ≈ ±9.2 × 10¹⁸       | When n > 10⁹ or products/sums can overflow |
| `double`    | 8 bytes | \~15 decimal digits | Floating point problems                    |
| `char`      | 1 byte  | -128 to 127         | Single characters                          |
| `bool`      | 1 byte  | true/false          | Flags                                      |

<Warning>
  **Integer overflow** is the #1 beginner mistake. If the problem says n ≤ 10⁹, then `n * n` overflows `int`. Use `long long`!
</Warning>

## Basic math operations

```cpp theme={null}
// 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.

```cpp theme={null}
// 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.

```cpp theme={null}
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.

```cpp theme={null}
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](/level-0/sheets/sheet-1) to practice these concepts with curated problems.

<Card title="Sheet 1: Intro to PS" icon="list-check" href="/level-0/sheets/sheet-1" horizontal>
  Practice problems for this tutorial.
</Card>
