Expressions and Linear Equations
Pillar: FIX — "Hold one quantity still, solve for the other."
Why Algebra Matters for Problem Solving
Every competitive programming problem, at its core, asks you to find unknown values that satisfy certain constraints. That is exactly what algebra does. When a problem says "find the minimum number of operations" or "count the pairs that sum to \(K\)," you are solving equations — sometimes simple, sometimes layered inside loops and recursion, but equations nonetheless.
This lesson builds the skill of translating a situation into an equation and then solving it mechanically. If you can do this fluently, you will read problems faster, spot the mathematical structure sooner, and avoid the "I see the problem but don't know how to start" paralysis.
Expressions: The Language of Computation
An expression is a combination of numbers, variables, and operations that represents a value.
This says: "take some number \(x\), multiply it by 3, then add 5." The expression doesn't ask a question — it just describes a quantity. When we set an expression equal to something, we get an equation, which does ask a question: "what value of \(x\) makes this true?"
Order of Operations
Expressions are evaluated in a strict order. The mnemonic PEMDAS captures it:
| Priority | Operation | Example |
|---|---|---|
| 1 | Parentheses | \((2 + 3) \times 4 = 20\) |
| 2 | Exponents | \(2^3 + 1 = 9\) |
| 3 | Multiplication / Division (left to right) | \(6 / 2 \times 3 = 9\) |
| 4 | Addition / Subtraction (left to right) | \(5 - 2 + 1 = 4\) |
A common source of bugs in code: forgetting that a + b * c computes b * c first. In both math and C++, multiplication binds tighter than addition. When in doubt, add parentheses.
Evaluating Expressions
To evaluate an expression, substitute the value and follow the order of operations.
Example: Evaluate \(2x^2 - 3x + 1\) when \(x = 4\).
Step by step: exponent first (\(4^2 = 16\)), then multiplications (\(2 \times 16\), \(3 \times 4\)), then left-to-right addition/subtraction.
Simplifying Expressions
Before solving equations, you often need to simplify expressions by combining like terms and distributing.
Like Terms
Like terms have the same variable raised to the same power. You can add or subtract their coefficients.
But \(5x + 3x^2\) cannot be simplified — different powers of \(x\) are different terms.
The Distributive Property
The distributive property lets you multiply through parentheses:
Example: Simplify \(3(2x + 4) - 2(x - 1)\).
Notice the minus sign distributes to both terms inside the second parentheses: \(-2(x - 1) = -2x + 2\). Sign errors here are the most common algebra mistake — and the most common source of wrong answers on contest problems.
Solving One-Variable Linear Equations
A linear equation in one variable has the form \(ax + b = c\) where \(a \neq 0\). "Linear" means the variable appears only to the first power — no \(x^2\), no \(\sqrt{x}\), no \(1/x\).
The Golden Rule
Whatever you do to one side of an equation, you must do to the other side. An equation is a balance scale. Adding 5 to the left but not the right tips the scale.
The Two-Step Method
Most linear equations reduce to two steps:
- Isolate the variable term — move everything else to the other side.
- Divide by the coefficient — get the variable alone.
Example: Solve \(5x - 3 = 22\).
Check: \(5(5) - 3 = 25 - 3 = 22\). Correct.
Multi-Step Equations
When variables appear on both sides, collect them first.
Example: Solve \(3x + 7 = x + 15\).
Example with distribution: Solve \(2(3x - 1) = 4x + 6\).
Fractions in Equations
When an equation has fractions, multiply both sides by the LCD (least common denominator) to clear them.
Example: Solve \(\frac{x}{3} + \frac{x}{4} = 7\).
LCD of 3 and 4 is 12. Multiply everything by 12:
This technique of clearing denominators shows up constantly in competition math — whenever you see fractions in a Diophantine equation or a modular arithmetic problem, your first instinct should be to clear them.
Word Problems: Translating English to Algebra
The hardest part of algebra is not solving equations — it is setting them up. Competition problems never say "solve \(3x + 5 = 20\)." They describe a situation and you must extract the equation yourself.
The Translation Table
| English phrase | Algebraic translation |
|---|---|
| "a number" / "some value" | \(x\) |
| "5 more than \(x\)" | \(x + 5\) |
| "3 less than \(x\)" | \(x - 3\) |
| "twice \(x\)" / "double \(x\)" | \(2x\) |
| "the product of \(x\) and \(y\)" | \(xy\) |
| "the sum of \(x\) and \(y\) is 10" | \(x + y = 10\) |
| "\(x\) is 4 more than \(y\)" | \(x = y + 4\) |
| "the ratio of \(x\) to \(y\) is 3:2" | \(x/y = 3/2\) or \(2x = 3y\) |
Worked Example
Problem: A rope is cut into two pieces. The longer piece is 3 meters more than twice the shorter piece. The total length is 27 meters. Find the length of each piece.
Step 1: Define the variable. Let \(s\) = length of the shorter piece.
Step 2: Express other quantities in terms of the variable. The longer piece is \(2s + 3\).
Step 3: Write the equation. Total length = shorter + longer:
Step 4: Solve.
The shorter piece is 8 m, the longer piece is \(2(8) + 3 = 19\) m.
Step 5: Check. \(8 + 19 = 27\). Correct. \(19 = 2(8) + 3\). Correct.
Why This Matters in CP
In competitive programming, the "word problem" is the problem statement itself. When Codeforces says "Alice and Bob take turns. Alice removes at most \(k\) stones each turn. After \(n\) turns the total removed is \(T\)..." — you need to translate that into constraints on variables, set up equations or inequalities, and solve. The translation skill is not optional.
Two-Variable Linear Equations
A linear equation in two variables has the form \(ax + by = c\). Its solutions are not a single number but a set of \((x, y)\) pairs.
Example: \(x + y = 10\) has infinitely many solutions: \((0, 10)\), \((1, 9)\), \((3, 7)\), \((-2, 12)\), etc.
When a problem gives you one equation in two unknowns, you cannot find unique values. You need a second equation. This is a system of equations, which we cover in full in Lesson 4. For now, the key insight is:
One equation, one unknown = solvable. Two unknowns need two equations.
This is the FIX move in action. If someone tells you \(x = 3\), you have fixed one variable, and the equation \(x + y = 10\) collapses to \(3 + y = 10\), giving \(y = 7\). Fixing eliminates a degree of freedom.
Integer Solutions
In competition math, we often want integer solutions to equations like \(3x + 5y = 47\) where \(x, y \geq 0\). You can fix one variable and check whether the other comes out as a non-negative integer.
Example: Find all non-negative integer solutions to \(3x + 5y = 47\).
Fix \(y = 0\): \(3x = 47\). Not divisible. No solution.
Fix \(y = 1\): \(3x = 42\), \(x = 14\). Solution: \((14, 1)\).
Fix \(y = 2\): \(3x = 37\). Not divisible. No solution.
Fix \(y = 4\): \(3x = 27\), \(x = 9\). Solution: \((9, 4)\).
Fix \(y = 7\): \(3x = 12\), \(x = 4\). Solution: \((4, 7)\).
The pattern: \(y\) must satisfy \(47 - 5y \equiv 0 \pmod{3}\), i.e., \(5y \equiv 47 \pmod{3}\), i.e., \(2y \equiv 2 \pmod{3}\), so \(y \equiv 1 \pmod{3}\). The solutions are \(y = 1, 4, 7\) with corresponding \(x = 14, 9, 4\).
This is a preview of modular arithmetic (Chapter 4) and Diophantine equations (Chapter 3). The algebra you are learning now is the foundation those chapters build on.
Common Pitfalls
| Mistake | Example | Correction |
|---|---|---|
| Sign error in distribution | \(-2(x - 3) = -2x - 6\) | \(-2(x - 3) = -2x + 6\) |
| Forgetting to apply operation to both sides | \(x + 3 = 7 \Rightarrow x = 7 - 3\) is fine, but some students subtract 3 only from \(x\) | Always subtract from the entire side |
| Dividing by a variable | If \(xy = 5x\), dividing by \(x\) gives \(y = 5\) but loses the solution \(x = 0\) | Factor instead: \(x(y - 5) = 0\) |
| Mixing up "less than" | "3 less than \(x\)" is \(x - 3\), not \(3 - x\) | Read carefully: "A less than B" means \(B - A\) |
Practice Problems
Problem 1. Solve \(4(2x - 3) + 5 = 3(x + 2) - 1\).
Solution
Distribute: \(8x - 12 + 5 = 3x + 6 - 1\)
Simplify: \(8x - 7 = 3x + 5\)
Collect: \(5x = 12\)
Problem 2. Solve \(\frac{2x + 1}{3} = \frac{x - 2}{2} + 1\).
Solution
LCD is 6. Multiply through:
\(2(2x + 1) = 3(x - 2) + 6\)
\(4x + 2 = 3x - 6 + 6\)
\(4x + 2 = 3x\)
\(x = -2\)
Check: LHS \(= \frac{2(-2)+1}{3} = \frac{-3}{3} = -1\). RHS \(= \frac{-2-2}{2} + 1 = -2 + 1 = -1\). Correct.
Problem 3. The sum of three consecutive integers is 96. Find them.
Solution
Let the integers be \(n\), \(n+1\), \(n+2\).
\(n + (n+1) + (n+2) = 96\)
\(3n + 3 = 96\)
\(3n = 93\)
\(n = 31\)
The integers are 31, 32, 33.
Problem 4. Find all non-negative integer solutions to \(7x + 3y = 50\).
Solution
Fix \(x\) and check if \(y\) is a non-negative integer:
\(y = \frac{50 - 7x}{3}\)
We need \(50 - 7x \geq 0\) and \(50 - 7x \equiv 0 \pmod{3}\).
\(50 - 7x \equiv 2 - x \pmod{3}\), so we need \(x \equiv 2 \pmod{3}\).
Also \(x \leq 7\) (since \(7 \times 8 = 56 > 50\)).
\(x = 2\): \(y = \frac{36}{3} = 12\). Solution: \((2, 12)\).
\(x = 5\): \(y = \frac{15}{3} = 5\). Solution: \((5, 5)\).
Solutions: \((2, 12)\) and \((5, 5)\).
Problem 5 (Competition Style). A number \(N\) satisfies: when you add 7 and divide by 3, the result is the same as when you subtract 5 and divide by 2. Find \(N\).
Solution
Translate: \(\frac{N + 7}{3} = \frac{N - 5}{2}\)
Cross-multiply: \(2(N + 7) = 3(N - 5)\)
\(2N + 14 = 3N - 15\)
\(29 = N\)
Check: \(\frac{36}{3} = 12\) and \(\frac{24}{2} = 12\). Correct.