Finding the furthest point in a grid when compared to other points
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
A common grid problem is to find the point whose nearest reference point is as far away as possible. In other words, for every candidate cell, you compute its distance to the closest point in a given set, then choose the candidate with the maximum of those minimum distances.
Define the Distance Model First
The right algorithm depends on what "distance" means.
On a grid, the most common choice is Manhattan distance:
That fits four-direction movement on an unweighted grid. If your problem uses diagonal movement or true geometric distance, the implementation changes.
Brute Force for Small Grids
For a small grid, the direct solution is easy to write: evaluate every cell against every reference point.
This is correct and often good enough if the grid is small. Its weakness is cost: every candidate cell compares itself against every source point.
Multi-Source BFS for Unweighted Grids
If movement is four-directional and every step has equal cost, a more scalable solution is multi-source breadth-first search. Start the BFS from all reference points at once. The last cell reached is one of the furthest points.
This works because BFS explores cells in increasing distance order. Starting from all sources simultaneously means each cell is assigned the distance to its nearest source automatically.
Obstacles Change the Meaning
If some cells are blocked, BFS becomes even more useful because Manhattan distance no longer reflects actual travel cost. In that case, the furthest reachable point is the cell with the maximum BFS distance from the nearest source while respecting walls.
That is one reason it helps to think of the problem as shortest-path distance on a grid graph, not just a formula over coordinates.
Tie Handling
Several cells may be equally far from the source set. Decide the tie rule explicitly:
- first cell found
- smallest row then column
- all furthest cells
Many algorithms are correct mathematically but still fail tests because the tie-breaking rule was not defined.
Common Pitfalls
- Using Manhattan distance when the real problem includes blocked cells or weighted movement.
- Solving with brute force on a large grid when multi-source BFS would be much faster.
- Forgetting that the objective is maximum distance to the nearest source, not maximum distance to any source.
- Ignoring tie rules when several cells share the same best distance.
- Treating the problem as Euclidean geometry when it is really a graph-distance problem on a grid.
Summary
- The target cell maximizes its minimum distance to the given reference points.
- For small grids, brute force is easy and correct.
- For unweighted grid movement, multi-source BFS is the standard scalable solution.
- Obstacles make graph distance more important than simple coordinate formulas.
- Define the distance metric and tie-breaking rule before choosing an algorithm.
Related reading
- Finding the hundred largest numbers in a file of a billion
- Finding the index of a given permutation
- Finding the intersecting node from two intersecting linked lists
- Finding the kth quantiles of an n-elements set. From cormen
- Finding the LCM of a range of numbers
- Finding the line along the intersection of two planes
- Finding the largest subarray with equal number of 0's and 1's
- Finding the longest border of a string

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.