Find rectangles that contain point – Efficient Algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computational geometry, a problem frequently encountered is finding all rectangles within a set that contain a given point efficiently. This is particularly relevant in geographic information systems (GIS), computer graphics, game development, and data visualization. The complexity of the problem scales with the number of rectangles and requires efficient algorithms to ensure quick query responses even with large datasets.
Understanding the Problem
Imagine you have a plane with several axis-aligned rectangles, and you want to determine which rectangles contain a specific point . Naively iterating over all rectangles and checking if each one contains the point leads to a time complexity of per query, where is the number of rectangles. This method becomes inefficient as grows large. Thus, an efficient algorithm is necessary for applications requiring fast query response times.
Efficient Algorithm Overview
To handle this problem more efficiently, data structures like Range Trees, Interval Trees, Segment Trees, or Quad Trees can be utilized. These structures allow preprocessing of rectangles into a form that supports logarithmic time complexity for each query, typically reduced to , where is the number of rectangles containing the point.
Range Tree Approach
- Construction:
- First, sort the rectangles based on their x-coordinates.
- Build a balanced binary tree (BST) – a 3D range tree structure.
- Store each rectangle in the tree based on its x-coordinate range, with the leaf nodes holding the rectangles themselves.
- Node construction occurs in time, with space complexity.
- Querying:
- Given a point , you can locate the node corresponding to the x-coordinate in .
- For each relevant node, examine its y-coordinates using secondary structures like secondary segment or interval trees.
- This approach ensures efficient querying in .
Technical Implementation Example
- Efficiency: Enhanced query processing time significantly reduces lookup time from linear to logarithmic scales.
- Scalability: Can handle large datasets effectively due to reduction in query time.
- Applicability: Adapts to both 2D and 3D problems, with extended capabilities in multidimensional space querying.
Related reading
- Find running median from a stream of integers
- Find Second largest number in array at most nlog₂n−2 comparisons
- Find set of numbers in one collection that adds up to a number in another
- Find Shortest Binary String In Given Interval
- Find shortest subarray containing all elements
- Find the 2nd largest element in an array with minimum number of comparisons
- find smallest area that contains all the rectangles
- Find the best combination from a given set of multiple sets

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.