Skip to content

Practice Difference of Number of Distinct Values on Diagonals

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2711
LeetCode Problem

Difference of Number of Distinct Values on Diagonals

Problem Statement

For each cell (r, c) in grid, count the distinct values strictly above-left on its diagonal, and separately count the distinct values strictly below-right on that diagonal. Exclude the cell itself from both counts. Set answer[r][c] to the absolute difference of these counts and return answer.

Source: Difference of Number of Distinct Values on Diagonals on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[1,2,3],[3,1,5],[3,2,1]]
Output
[[1,1,0],[1,0,1],[0,1,1]]

# Example 2

Input
grid = [[1]]
Output
[[0]]

Constraints

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

Related LeetCode Problems

Practice more problems similar to Difference of Number of Distinct Values on Diagonals.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.