Difference of Number of Distinct Values on Diagonals - Problem

Given a 2D grid of size m x n, you should find the matrix answer of size m x n.

The cell answer[r][c] is calculated by looking at the diagonal values of the cell grid[r][c]:

  • Let leftAbove[r][c] be the number of distinct values on the diagonal to the left and above the cell grid[r][c] not including the cell grid[r][c] itself.
  • Let rightBelow[r][c] be the number of distinct values on the diagonal to the right and below the cell grid[r][c], not including the cell grid[r][c] itself.
  • Then answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|.

A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until the end of the matrix is reached.

Return the matrix answer.

Input & Output

Example 1 — Basic 3x3 Grid
$ Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[2,1,0],[1,0,1],[0,1,2]]
💡 Note: For cell (1,1) with value 5: left/above has {1} (1 distinct), right/below has {9} (1 distinct), so |1-1|=0. For cell (0,0): no left/above (0 distinct), right/below has {5,9} (2 distinct), so |0-2|=2.
Example 2 — Small 2x2 Grid
$ Input: grid = [[1,2],[3,4]]
Output: [[1,0],[0,1]]
💡 Note: Cell (0,0): no left/above, right/below has {4}, difference=1. Cell (1,1): left/above has {1}, no right/below, difference=1.
Example 3 — Duplicate Values
$ Input: grid = [[1,1,1],[1,1,1],[1,1,1]]
Output: [[0,0,0],[0,0,0],[0,0,0]]
💡 Note: All values are the same, so each diagonal has at most 1 distinct value. Every cell will have equal counts on both sides, resulting in difference 0.

Constraints

  • 1 ≤ m, n ≤ 50
  • 1 ≤ grid[i][j] ≤ 50

Visualization

Tap to expand
Diagonal Difference Calculation INPUT GRID 1 2 3 4 5 6 7 8 9 leftAbove rightBelow For cell [1][1] (value 5): leftAbove: {1} = 1 distinct rightBelow: {9} = 1 distinct |1 - 1| = 0 Input: [[1,2,3],[4,5,6],[7,8,9]] ALGORITHM STEPS 1 Group by Diagonal Cells with same (r-c) are on same diagonal 2 Traverse Each Diagonal Top-left to bottom-right Track distinct values 3 Count Left-Above Use Set for distinct values before cell 4 Count Right-Below Traverse reverse Count after cell Same color = same diagonal FINAL RESULT 2 1 0 1 0 1 0 1 2 answer[r][c] = |leftAbove - rightBelow| Corner cells: max diff Center cell: balanced Output: [[2,1,0],[1,0,1],[0,1,2]] OK Key Insight: Cells on the same diagonal share the same (row - column) difference value. By preprocessing each diagonal with two passes (forward and backward), we can compute distinct counts efficiently in O(m*n) time using Sets for each diagonal. TutorialsPoint - Difference of Number of Distinct Values on Diagonals | Diagonal Preprocessing
Asked in
Google 25 Microsoft 18 Amazon 15 Facebook 12
28.5K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen