Count Number of Triples in an array that are collinear
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To count collinear triples, you need to count how many groups of three points lie on the same straight line. The brute-force solution checks every triple directly in O(n^3), but a more useful approach fixes one anchor point at a time, groups other points by slope, and then counts combinations.
Collinearity Test for Three Points
Three points are collinear when the signed area of the triangle they form is zero. For points (x1, y1), (x2, y2), and (x3, y3), the test is:
This avoids division, so it is safer than comparing floating-point slopes directly.
Brute Force Baseline
The direct solution tries every triple:
This is easy to understand and correct, but it runs in O(n^3), which becomes expensive quickly.
A Better Counting Idea
Instead of testing every triple explicitly, fix one point as an anchor and group all other points by the slope they make with that anchor.
If m other points share the same slope relative to the anchor, then they contribute:
collinear triples involving that anchor, because any two of those m points combined with the anchor form a triple on the same line.
The catch is that each triple gets counted once from each of its three points as anchor, so the final total must be divided by 3.
Normalized Slope Representation
You should not use raw floating-point slopes as dictionary keys. Instead, normalize the direction vector with a greatest-common-divisor reduction.
This makes equivalent slopes hash to the same key, including negative directions.
An O(n^2) Counting Approach
This runs in O(n^2) slope grouping time plus hashing overhead, which is much better than brute force for larger inputs.
Why Divide by Three
Suppose points A, B, and C are collinear. The anchor method counts them:
- once with
Aas anchor - once with
Bas anchor - once with
Cas anchor
So every true triple is counted three times. Dividing by 3 fixes that overcounting.
For lines with more than three points, the same correction still works because the anchor-based combination count overcounts every triple exactly three times.
Common Pitfalls
- Using floating-point slopes directly and getting precision bugs.
- Forgetting to normalize slope direction, which splits equivalent lines into different hash keys.
- Counting anchor-based combinations and forgetting to divide the final answer by
3. - Assuming the brute-force solution is acceptable for large
njust because the collinearity test itself is cheap. - Ignoring duplicate points, which may require separate handling depending on the problem definition.
Summary
- Three points are collinear when the triangle area test evaluates to zero.
- Brute force works in
O(n^3)by checking every triple directly. - A more efficient method fixes one anchor, groups other points by normalized slope, and counts combinations.
- Use reduced integer slope pairs instead of floating-point values.
- The anchor-based total must be divided by
3because each triple is counted once from each of its three points.
Related reading
- Count of co-prime pairs from two arrays in less than On2 complexity
- Count points in a rectangle
- Count points inside triangle fast
- Count sum of multiples of a number below N with O1 complexity?
- count the number of distinct absolute values among the elements of the array
- Count the number of set bits in a 32-bit integer
- Count the number of Ks between 0 and N
- Count the subsequences of length 4 divisible by 9

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.