Skip to main content

Manhattan Distances of All Arrangements of Pieces - Solution & Explanation

HardMathCombinatorics4 min readAsked at: Amazon, Google, Rubrik
Practice this problem

Problem Statement

You are given three integers m, n, and k.

There is a rectangular grid of size m × n containing k identical pieces. Return the sum of Manhattan distances between every pair of pieces over all valid arrangements of pieces.

A valid arrangement is a placement of all k pieces on the grid with at most one piece per cell.

Since the answer may be very large, return it modulo 109 + 7.

The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

 

Example 1:

Input: m = 2, n = 2, k = 2

Output: 8

Explanation:

The valid arrangements of pieces on the board are:

  • In the first 4 arrangements, the Manhattan distance between the two pieces is 1.
  • In the last 2 arrangements, the Manhattan distance between the two pieces is 2.

Thus, the total Manhattan distance across all valid arrangements is 1 + 1 + 1 + 1 + 2 + 2 = 8.

Example 2:

Input: m = 1, n = 4, k = 3

Output: 20

Explanation:

The valid arrangements of pieces on the board are:

  • The first and last arrangements have a total Manhattan distance of 1 + 1 + 2 = 4.
  • The middle two arrangements have a total Manhattan distance of 1 + 2 + 3 = 6.

The total Manhattan distance between all pairs of pieces across all arrangements is 4 + 6 + 6 + 4 = 20.

 

Constraints:

  • 1 <= m, n <= 105
  • 2 <= m * n <= 105
  • 2 <= k <= m * n

Approach Overview

Problem Overview: You place k identical pieces on an m x n grid. For every possible arrangement, compute the total Manhattan distance between every pair of pieces. The task is to return the sum of these distances across all valid arrangements.

Approach 1: Brute Force Enumeration (Exponential)

The most direct idea is to generate every way to place k pieces among the m * n cells, then compute the pairwise Manhattan distance for each arrangement. For every configuration you iterate over all k choose 2 pairs and accumulate distances. This quickly becomes infeasible because the number of arrangements is C(mn, k). Time complexity is roughly O(C(mn, k) * k^2) with O(k) space for storing positions. This approach only works for extremely small grids and mainly helps you understand what the problem is asking.

Approach 2: Pair Contribution with Combinatorics (O(m + n))

Instead of enumerating arrangements, focus on how much each pair of cells contributes to the final answer. If two cells are chosen for pieces, their Manhattan distance contributes once for every arrangement where those two cells are occupied. The number of such arrangements is C(mn - 2, k - 2) because the remaining k-2 pieces can be placed in any of the other cells.

Now compute the sum of Manhattan distances across all unordered cell pairs. Manhattan distance splits cleanly into row and column components: |r1 - r2| + |c1 - c2|. You can aggregate these independently.

For rows, consider every distance d between two rows. There are (m - d) row pairs with that difference. Each row pair combines with any column pair, giving n * n cell pairs. The total row contribution becomes sum(d * (m - d) * n^2). Do the same for columns: sum(d * (n - d) * m^2). This produces the total Manhattan distance across all cell pairs in O(m + n) time.

Finally multiply that value by C(mn - 2, k - 2). Precompute factorials and modular inverses to evaluate combinations efficiently. This solution relies heavily on combinatorics and mathematical decomposition of Manhattan distance. Time complexity is O(m + n) with O(mn) preprocessing for factorials.

Recommended for interviews: The combinatorics contribution approach is what interviewers expect. Explaining the brute force shows you understand the problem space, but recognizing that each pair of cells contributes independently demonstrates strong mathematical reasoning and familiarity with combinatorial counting.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(C(mn, k) * k^2)O(k)Only for very small grids or conceptual understanding
Pair Contribution with CombinatoricsO(m + n)O(mn) for factorial precomputationOptimal solution for large constraints and interview settings

Video Solution

Leetcode 3426. Manhattan Distances of All Arrangements of Pieces |Math Proof | Biweekly Contest 148 • By IITians • 676 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Manhattan Distances of All Arrangements of Pieces easy or hard?
Manhattan Distances of All Arrangements of Pieces is classified as a Hard problem. The difficulty comes from recognizing that direct enumeration is impossible and converting the problem into a combinatorics-based contribution calculation with efficient modular arithmetic.
Manhattan Distances of All Arrangements of Pieces Python/Java solution
Python, Java, C++, and Go implementations follow the same structure: precompute factorials and modular inverses, compute row and column distance sums using combinatorial formulas, then multiply by C(mn - 2, k - 2). The algorithm avoids enumerating placements and runs in near linear time relative to grid dimensions.
How to solve Manhattan Distances of All Arrangements of Pieces in O(m+n)?
Break the Manhattan distance into independent row and column components. For rows, sum d * (m - d) * n^2 for every row difference d. For columns, sum d * (n - d) * m^2. This gives the total Manhattan distance across all cell pairs. Multiply the result by C(mn - 2, k - 2) to account for the remaining piece placements.
What is the best approach for Manhattan Distances of All Arrangements of Pieces?
The best approach uses combinatorics with pair contribution analysis. Instead of enumerating arrangements, compute the Manhattan distance for every pair of grid cells and multiply by the number of ways the remaining pieces can be placed. This reduces the problem to summing row and column distance contributions and multiplying by C(mn-2, k-2). The overall complexity is O(m + n) after factorial preprocessing.
Is Manhattan Distances of All Arrangements of Pieces asked at Google/Amazon/Meta?
Hard combinatorics and mathematical counting problems like this commonly appear in interviews at companies such as Google, Amazon, and Meta. They test the ability to transform brute force enumeration into a mathematical counting formula using combinatorics and distance decomposition.
What data structure is used in Manhattan Distances of All Arrangements of Pieces?
The solution primarily relies on mathematical computation rather than complex data structures. Arrays are typically used to store factorials and inverse factorials for fast combination calculations, while the core logic uses combinatorics and arithmetic aggregation.
What is the time complexity of Manhattan Distances of All Arrangements of Pieces?
The optimal solution runs in O(m + n) time for computing row and column distance contributions, plus O(mn) preprocessing to build factorials and inverse factorials for combinations. The brute force approach would require O(C(mn, k) * k^2) time, which is infeasible for typical constraints.

Ready to solve this problem?

Practice Manhattan Distances of All Arrangements of Pieces with our built-in code editor and test cases.

Practice on FleetCode