Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k.
The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexed).
Find the kth largest value (1-indexed) of all the coordinates of matrix.
Example 1:
Input: matrix = [[5,2],[1,6]], k = 1 Output: 7 Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
Example 2:
Input: matrix = [[5,2],[1,6]], k = 2 Output: 5 Explanation: The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.
Example 3:
Input: matrix = [[5,2],[1,6]], k = 3 Output: 4 Explanation: The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10000 <= matrix[i][j] <= 1061 <= k <= m * nProblem summary: You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexed). Find the kth largest value (1-indexed) of all the coordinates of matrix.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[[5,2],[1,6]] 1
[[5,2],[1,6]] 2
[[5,2],[1,6]] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1738: Find Kth Largest XOR Coordinate Value
class Solution {
public int kthLargestValue(int[][] matrix, int k) {
int m = matrix.length, n = matrix[0].length;
int[][] s = new int[m + 1][n + 1];
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
ans.add(s[i + 1][j + 1]);
}
}
Collections.sort(ans);
return ans.get(ans.size() - k);
}
}
// Accepted solution for LeetCode #1738: Find Kth Largest XOR Coordinate Value
func kthLargestValue(matrix [][]int, k int) int {
m, n := len(matrix), len(matrix[0])
s := make([][]int, m+1)
for i := range s {
s[i] = make([]int, n+1)
}
var ans []int
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
s[i+1][j+1] = s[i+1][j] ^ s[i][j+1] ^ s[i][j] ^ matrix[i][j]
ans = append(ans, s[i+1][j+1])
}
}
sort.Ints(ans)
return ans[len(ans)-k]
}
# Accepted solution for LeetCode #1738: Find Kth Largest XOR Coordinate Value
class Solution:
def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
m, n = len(matrix), len(matrix[0])
s = [[0] * (n + 1) for _ in range(m + 1)]
ans = []
for i in range(m):
for j in range(n):
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j]
ans.append(s[i + 1][j + 1])
return nlargest(k, ans)[-1]
// Accepted solution for LeetCode #1738: Find Kth Largest XOR Coordinate Value
struct Solution;
impl Solution {
fn kth_largest_value(matrix: Vec<Vec<i32>>, k: i32) -> i32 {
let n = matrix.len();
let m = matrix[0].len();
let k = k as usize;
let mut xor = vec![vec![0; m]; n];
xor[0][0] = matrix[0][0];
let mut arr = vec![];
for i in 1..n {
xor[i][0] = xor[i - 1][0] ^ matrix[i][0];
}
for j in 1..m {
xor[0][j] = xor[0][j - 1] ^ matrix[0][j];
}
for i in 1..n {
for j in 1..m {
xor[i][j] = xor[i - 1][j - 1] ^ xor[i][j - 1] ^ xor[i - 1][j] ^ matrix[i][j];
}
}
for i in 0..n {
for j in 0..m {
arr.push(xor[i][j]);
}
}
arr.select_nth_unstable(n * m - k);
arr[n * m - k]
}
}
#[test]
fn test() {
let matrix = vec_vec_i32![[5, 2], [1, 6]];
let k = 1;
let res = 7;
assert_eq!(Solution::kth_largest_value(matrix, k), res);
let matrix = vec_vec_i32![[5, 2], [1, 6]];
let k = 2;
let res = 5;
assert_eq!(Solution::kth_largest_value(matrix, k), res);
let matrix = vec_vec_i32![[5, 2], [1, 6]];
let k = 3;
let res = 4;
assert_eq!(Solution::kth_largest_value(matrix, k), res);
let matrix = vec_vec_i32![[5, 2], [1, 6]];
let k = 4;
let res = 0;
assert_eq!(Solution::kth_largest_value(matrix, k), res);
}
// Accepted solution for LeetCode #1738: Find Kth Largest XOR Coordinate Value
function kthLargestValue(matrix: number[][], k: number): number {
const m: number = matrix.length;
const n: number = matrix[0].length;
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
const ans: number[] = [];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
ans.push(s[i + 1][j + 1]);
}
}
ans.sort((a, b) => b - a);
return ans[k - 1];
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.