Count points in a rectangle
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
Counting how many points lie inside a rectangle is a classic range-query problem in computational geometry. The right solution depends on whether you have one query or many: a single query can be answered by scanning all points, while many queries justify preprocessing so each rectangle can be answered much faster.
Naive Scan for One-Off Queries
If you only need to answer a rectangle count occasionally, the simplest solution is to test each point directly.
This runs in O(n) time per query. That is perfectly fine when the dataset is small or the number of rectangle queries is low.
Use a 2D Prefix Sum for Many Queries on a Grid
If point coordinates are integer grid positions inside a bounded range and you need many rectangle queries, a 2D prefix sum is a strong option. First build a grid of point counts, then precompute cumulative sums.
After preprocessing, each rectangle query is O(1). The tradeoff is that this approach assumes bounded integer coordinates or coordinate compression.
When Coordinates Are Large or Continuous
If coordinates are large, sparse, or continuous, a dense grid may waste too much memory. In that case, more advanced data structures such as range trees, Fenwick trees with coordinate compression, or segment-tree-based sweep-line methods become more attractive.
The important decision is not which data structure sounds most sophisticated. It is whether your workload is:
- one query versus many queries
- dense grid versus sparse coordinates
- static points versus points that change over time
Those constraints determine the right algorithm much more than the rectangle query itself.
That is why range-query problems are usually discussed together with workload assumptions. The same mathematical question can justify a simple loop, a prefix table, or an advanced spatial index depending on how often the query is asked and how large the point set becomes.
Common Pitfalls
- Using an
O(n)scan for a workload with thousands of repeated queries. - Building a dense grid when coordinates are huge and sparse.
- Forgetting to normalize rectangle corners when
x1 > x2ory1 > y2. - Miscounting boundary points by using strict comparisons instead of inclusive ones.
- Choosing a complex data structure before understanding the query pattern.
Summary
- A direct scan is the simplest answer for one-off rectangle queries.
- A 2D prefix sum is excellent for many queries on bounded integer grids.
- Sparse or large coordinates usually need coordinate compression or other range-query structures.
- Always normalize rectangle bounds before counting.
- Match the algorithm to the workload, not just to the geometry problem statement.
Related reading
- 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 Ks between 0 and N
- Count the number of set bits in a 32-bit integer
- Counting alternating numbers in an array
- Count the subsequences of length 4 divisible by 9
- count the total number of 1's in integers from 1 to N

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.