Guide · Algorithm examples

Algorithm Examples – a Guide to the Tasks

Tasks in which it is not enough to just "write a program" – you have to come up with a good method: mathematical algorithms, speeding up a program with a formula, recursion, generating combinatorial objects and search with going back (backtracking).

Before these tasks you should confidently solve the tasks from the basic examples – especially loops, arrays and functions.

  • Grades 7–8 and secondary school
  • C++
  • Preparation for the district and national competition
Tips: before writing code, estimate how many operations the program does for the largest input allowed by the constraints. About 108 simple operations take roughly one second – if your method is slower, look for a formula or a better algorithm.

Some lessons linked below are available only in Serbian for now. They are marked (SR).

1 Mathematical algorithms and complexity

GCD and LCM, prime numbers, factorization and how to replace a slow loop with a formula.

2 Recursion

A function that calls itself: the power, the Fibonacci sequence, binary notation, the inverse matrix.

3 Combinatorics and backtracking

How to systematically list all possibilities and how to discard those that do not lead to a solution.

4 Lessons for these tasks

Theory worth reading before or while solving.

5 Next

Competition tasks by level.

Suggested order

  1. Algorithms: GCD and prime numbers
  2. Improving complexity (SR)
  3. Recursive algorithms
  4. Combinatorial problems (SR)
  5. Backtracking and brute force (SR)

Useful external resources

  • Petlja – the "Advanced algorithms" collections and competition tasks with automatic checking (in Serbian)
  • CSES Problem Set – classic algorithmic tasks, the Introductory and Complete Search sections
  • VisuAlgo – animations of algorithms: recursion, sorting, searching