Brute force tries possible answers directly. It is often the simplest correct solution before any optimization is added.

Brute force is useful because it makes the problem’s search space visible.

Core Idea

The algorithm lists candidates, checks each candidate, and returns the one that satisfies the condition. If there are many candidates, brute force can become too slow.

Even when it is not efficient enough, a brute-force solution often clarifies what a better algorithm must avoid repeating.

Python Example

def has_pair_sum(values, target):
    for i in range(len(values)):
        for j in range(i + 1, len(values)):
            if values[i] + values[j] == target:
                return True
    return False

This checks every pair. The time complexity is O(n^2).

Common Confusions

Brute force is not automatically wrong. It may be the right answer for small constraints or a useful first version.

The mistake is keeping brute force when the input size makes the number of candidates too large.

When To Use It

Use brute force to get a baseline, verify small test cases, understand the search space, or solve problems whose constraints are small enough for direct enumeration.