C++20 sections · 1024 units
Open in Course

Practice: Permutation Problems

Arrangement challenges

I'll give you a traveling salesman variant: find the shortest tour visiting nn cities. Use next_permutation to try all possible routes and track the minimum distance found. Sort city indices first, then loop through all permutations calculating distance for each route.

Keep track of the minimum total distance across all orderings you try. This brute force works for n10n \leq 10 cities. Larger inputs need dynamic programming or approximation algorithms since permutations grow factorially in count.