Magicsheet logo

Manhattan Distances of All Arrangements of Pieces

Hard
61%
Updated 6/1/2025

Asked by 3 Companies

Manhattan Distances of All Arrangements of Pieces

What is this problem about?

This is a highly advanced combinatorial math problem. You are given an M×NM \times N grid and KK identical pieces. You need to place all KK pieces on the grid. For a single arrangement of KK pieces, the "Manhattan Distance" of that arrangement is the sum of the Manhattan distances between all pairs of pieces. You must return the sum of these distances across all possible valid arrangements of the KK pieces on the grid, modulo 109+710^9 + 7.

Why is this asked in interviews?

This problem is asked almost exclusively in quantitative finance firms (like Citadel or HRT) or for extremely senior FAANG algorithmic roles. It tests a candidate's ability to decouple combinatorics from geometry. A brute-force generation of arrangements is impossible. It requires you to mathematically calculate the expected contribution of a single distance between two specific cells across all possible combinations of the remaining pieces.

Algorithmic pattern used

This problem relies purely on Combinatorics and Decoupling Dimensions. Instead of looking at whole arrangements, look at a single pair of cells A and B. The distance between them is dist(A, B). If we place pieces on A and B, how many ways can we place the remaining K2K - 2 pieces in the remaining M×N2M \times N - 2 empty cells? The answer is "N-choose-K": (M×N2K2)\binom{M \times N - 2}{K - 2}. Therefore, the total sum is simply the sum of dist(A, B) for all possible pairs of cells in the grid, multiplied by (M×N2K2)\binom{M \times N - 2}{K - 2}. Furthermore, calculating the sum of distances for all cell pairs can be optimized mathematically in O(M+N)O(M + N) or O(1)O(1) time by separating the X and Y axes.

Example explanation

Grid 2×22 \times 2, pieces K=3K = 3. Total cells C=4C = 4. Remaining cells to place K2K-2 pieces: C2=2C - 2 = 2. Remaining pieces: 32=13 - 2 = 1. Combinations = (21)=2\binom{2}{1} = 2. Let's find sum of all pairwise distances in a 2×22 \times 2 grid:

  • (0,0) to (0,1): dist 1
  • (0,0) to (1,0): dist 1
  • (0,0) to (1,1): dist 2 ... Doing this for all 6 unique pairs yields a total grid distance of 8. Total answer = 8×2=168 \times 2 = 16.

Common mistakes candidates make

The biggest mistake is attempting to simulate the board states or generate arrangements using backtracking. Since the board can be large and KK can be large, O((M×NK))O(\binom{M \times N}{K}) time complexity will fail instantly. Another error is calculating the combinations using naive factorials which overflow; you must use modular inverse properties to calculate (NK)(mod109+7)\binom{N}{K} \pmod{10^9+7}.

Interview preparation tip

For this specific advanced pattern, remember that Expected Value and Combinatorial Sums can almost always be "flipped". Instead of summing over configurations, sum over the atomic elements (the pairs of cells) and multiply by the number of configurations in which that atomic element is active. Also, learn how to calculate Sum of absolute differences in a 1D array mathematically to optimize the grid distance calculation.

Similar Questions