Ctrl K

Diagonal Variety Gap

Medium
You get an m × n table grid. Each cell lies on a diagonal running from the top-left towards the bottom-right. For a cell (r, c) let: - before be the number of **distinct** values on its diagonal above and to the left (cells (r-1, c-1), (r-2, c-2), …), - after be the number of distinct values on its diagonal below and to the right ((r+1, c+1), (r+2, c+2), …). The cell itself counts in neither. Return a table of the same size where cell (r, c) holds |before − after|.
Example 1
grid=[[4, 4, 2], [7, 4, 9], [3, 7, 4]] → [[1, 1, 0], [1, 0, 1], [0, 1, 1]]

Cell (0, 0): nothing up-left (0), down-right holds 4 and 4, one distinct value, |0 - 1| = 1. Cell (1, 1): up-left {4}, down-right {4}, so 0. Cell (2, 2): up-left holds 4 and 4, one distinct value, down-right is empty, so 1.

Example 2
grid=[[5, 6, 8, 5]] → [[0, 0, 0, 0]]

With a single row each cell is alone on its diagonal, so every answer is 0.

Example 3
grid=[[1, 2], [3, 1], [2, 3]] → [[1, 0], [1, 1], [0, 1]]
Constraints
  • 1 ≤ m, n ≤ 50
  • all rows have the same length
  • 1 ≤ grid[i][j] ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumZero Out the Crosses MediumMultiply Mostly-Zero Matrices MediumLonely Dark Pixels MediumDark Pixels in Twin Rows Medium
loading Python…
Input
grid=[[4, 4, 2], [7, 4, 9], [3, 7, 4]]
Expected
[[1, 1, 0], [1, 0, 1], [0, 1, 1]]

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.