Unique Pairs in a 2D Matrix Summing to Target
Problem statement
Given a rectangular integer matrix matrix whose values are globally unique and an integer target, return the number of unordered pairs of values whose sum is target.
Each pair must use two different cells. Count a value pair once regardless of order.
Function
countPairs(matrix: int[][], target: int) → intExamples
Example 1
matrix = [[1,5],[7,-1]]target = 6return = 2The unordered pairs are (1, 5) and (-1, 7).
Example 2
matrix = [[1,2],[3,4]]target = 5return = 2The pairs are (1, 4) and (2, 3).
Example 3
matrix = [[4]]target = 8return = 0The one value cannot be paired with its own cell.
Constraints
1 <= matrix.length, matrix[i].length.- The matrix is rectangular and contains at most
100000cells. -10^9 <= matrix[i][j], target <= 10^9.- All matrix values are distinct.