Skip to content
Algorithm Foundations
Path outline

Lesson

Direct reasoning

Translate a rule into a bounded search and justify its cost.

Start with the definition

An algorithm is a precise procedure for computing the answer. Before looking for a named technique, write down what makes an answer valid. Identify the input, the output, and the limits. Then try the most direct procedure and estimate how much work it performs.

A search can inspect every candidate when the candidate set is small. The important question is how many candidates there are. Visiting n values takes linear work. Checking all pairs can take roughly n squared work. Checking subsets of n values can take 2 to the n work. A method suitable for 10 values may be impossible for 100,000.

A complete example

Count pairs of distinct positions whose values add up to a target. This example uses a small list so checking every pair is reasonable.

Python 3
values = [2, 7, 4, 5]
target = 9
count = 0

for i in range(len(values)):
    for j in range(i + 1, len(values)):
        if values[i] + values[j] == target:
            count += 1

print(count)
text
2

The pairs are 2 with 7 and 4 with 5. Starting j at i + 1 prevents using the same position twice and prevents counting both orders of one pair. There are n * (n - 1) // 2 candidates. The running time is O(n squared), and the extra space is O(1), apart from the input list.

For a problem with a list supplied on one line, use values = list(map(int, input().split())). A list stores values in order and uses zero-based positions. len(values) gives its length. values[0] is its first element.

Explain why the search works

A correct search must cover every valid answer, reject invalid candidates, and avoid duplicates. In the example, every pair has one smaller position i and one larger position j. The loops reach that pair exactly once. The equality check then decides whether it counts.

For three chosen positions, use i < j < k. There are at most n cubed iterations with a simple triple loop. Problem 1433 has n at most 20, so this direct approach is small enough. The order of selection does not matter, which is why increasing positions are useful.

Useful arithmetic tools

An integer is odd when its remainder after division by 2 is 1. Two positive integers are coprime when their greatest common divisor is 1. Euclid's algorithm repeatedly replaces (a, b) with (b, a % b) until b is zero. Python provides the same operation as math.gcd.

The sum from 1 through n is n * (n + 1) // 2. Prove that formula by pairing terms. Replacing a loop with a justified formula can reduce linear work to a fixed number of arithmetic operations.

Subsets and pruning

Problem 1434 chooses a subset. A recursive search can either include or exclude each value. Since every value is positive, a branch whose total already exceeds the target can stop. That pruning would be invalid if negative numbers could later reduce the total. Check the constraints that justify a shortcut.

Practice

The sequence starts with a sum, then counts odd values, coprime values, triples, and subsets. Each step enlarges the candidate space. Write a sentence explaining what your loops count before submitting.

Archive difficulty is an estimate from solver performance, not a prerequisite or a guarantee. A low rating does not replace reading the statement. If you are stuck, compare your proposed work with the constraints and test the smallest valid case. Accepted confirms the tested output, while your explanation is how you establish that the algorithm covers the whole input range.

Sign in to save your progress.

Sign in

Practice