Brute-force
Solving Ttsks with Brute-force
Solving Tasks with Brute Force
Brute force is a method where we try all possible options and choose the best valid one. The idea is straightforward: define the search space — all candidates that could be the answer — and apply linear search over it, checking each one.
It is simple to implement but can be slow, so we use it only when the search space is small enough to scan entirely within the time limit.
The Core Idea
Every brute force solution follows the same structure:
- Define the search space — what are all possible answers?
- Iterate over it — one or more loops
- Check each candidate — does it satisfy the problem's conditions?
- Track the best/first/count — depending on what the problem asks
Example 1: Maximum Sum Subarray of Length K
Given a list of integers and a number , find the maximum sum of any contiguous subarray of length exactly .
Search space: all starting indices from to .