LeetCode solutions

1860. Find Kth Largest XOR Coordinate Value

My accepted C++ solution to LeetCode problem 1860, Find Kth Largest XOR Coordinate Value, running in 44ms.

  • Difficulty: Medium
  • C++
  • Runtime 44ms
  • Memory 130.8MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 44ms, memory 130.8MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    int kthLargestValue(vector<vector<int>>& matrix, int k) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> prefix(m + 1, vector<int>(n + 1, 0));
        vector<int> vals;

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                prefix[i][j] = prefix[i-1][j] ^ prefix[i][j-1] ^ prefix[i-1][j-1] ^ matrix[i-1][j-1];
                vals.push_back(prefix[i][j]);
            }
        }

        nth_element(vals.begin(), vals.begin() + k - 1, vals.end(), greater<int>());
        return vals[k - 1];
    }
};

Source