Difference of Number of Distinct Values on Diagonals - Problem

You're given a 2D grid of size m × n, and your task is to create an answer matrix of the same size where each cell contains a special diagonal difference calculation.

For each cell grid[r][c], you need to:

  • Count distinct values on the diagonal going left and above (not including the current cell)
  • Count distinct values on the diagonal going right and below (not including the current cell)
  • Calculate the absolute difference between these two counts

A diagonal line starts from either the topmost row or leftmost column and extends in the bottom-right direction until it reaches the matrix boundary.

Visual Example:

For cell at position (2,3), the diagonal contains cells that share the same row - col value. The red cells are left and above, blue cells are right and below.

Return the matrix where answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|

Input & Output

example_1.py — Basic 3x3 Grid
$ Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,1,0],[1,0,1],[0,1,1]]
💡 Note: For cell (1,1) with value 5: left-above diagonal has [1] (1 distinct), right-below has [9] (1 distinct), so |1-1| = 0
example_2.py — Repeated Values
$ Input: grid = [[1,2,2],[2,2,1],[1,1,2]]
Output: [[0,1,1],[1,0,1],[1,1,0]]
💡 Note: When values repeat on diagonals, distinct count may be less than total count. Center cell has left-above [1] and right-below [2], giving |1-1| = 0
example_3.py — Single Row/Column
$ Input: grid = [[1,2,3,4]]
Output: [[0,0,0,0]]
💡 Note: Each cell is on its own diagonal with no other elements, so both left-above and right-below counts are 0

Constraints

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

Visualization

Tap to expand
Matrix Diagonal Processing VisualizationOriginal Matrix123456789Diagonal GroupsDiag -2: (2,0) → [7]Diag -1: (1,0), (2,1) → [4,8]Diag 0: (0,0), (1,1), (2,2) → [1,5,9]Diag 1: (0,1), (1,2) → [2,6]Diag 2: (0,2) → [3]Processing Example: Center Cell (1,1)Left-Above: (0,0) = 1 → distinct count = 1Right-Below: (2,2) = 9 → distinct count = 1Result: |1 - 1| = 0Final Result Matrix110101011🚀 Optimized: O(m×n) time by processing each diagonal only once
Understanding the Visualization
1
Identify Diagonals
Group cells by their diagonal using (row - col) as the key
2
Process Each Diagonal
Traverse each diagonal once from top-left to bottom-right
3
Build Prefix Counts
Track distinct values accumulated from the beginning
4
Build Suffix Counts
Track distinct values accumulated from the end
5
Calculate Results
Use precomputed counts to find absolute differences
Key Takeaway
🎯 Key Insight: Grouping cells by diagonal (row-col) allows us to process each diagonal line exactly once, building prefix and suffix distinct counts efficiently rather than recalculating for every cell.
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
23.5K Views
Medium Frequency
~25 min Avg. Time
847 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