Program to find k-th largest XOR coordinate value in Python

Suppose we have one m x n matrix and another value k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i is in range (0 to a) and j is in range (0 to b). We have to find the k-th largest value (1-indexed) of all the coordinates of the matrix.

Problem Example

Consider the following matrix:

5 2
1 6

If k = 1, then the output will be 7 because the value of coordinate (0,1) is 5 XOR 2 = 7, and this is the largest coordinate value.

Algorithm Steps

To solve this problem, we will follow these steps −

  • m := row count, n := column count
  • For each row, compute prefix XOR values
  • For each column, compute prefix XOR values
  • Use a dictionary to track the k largest values
  • Return the minimum value from the k largest values

Solution Implementation

def solve(matrix, k):
    m, n = len(matrix), len(matrix[0])
    
    # Compute prefix XOR for each row
    for i in range(m):
        for j in range(n):
            if j:
                matrix[i][j] ^= matrix[i][j-1]

    seen = {}
    count = 0
    
    # Compute prefix XOR for each column and track k largest
    for i in range(n):
        for j in range(m):
            if j:
                matrix[j][i] ^= matrix[j-1][i]

            seen[matrix[j][i]] = seen.get(matrix[j][i], 0) + 1
            count += 1

            if count > k:
                min_value = min(seen)
                seen[min_value] -= 1
                if not seen[min_value]:
                    seen.pop(min_value)

    return min(seen)

# Test the function
matrix = [[5, 2], [1, 6]]
k = 1
result = solve(matrix, k)
print(f"The {k}th largest XOR coordinate value is: {result}")
The 1th largest XOR coordinate value is: 7

How It Works

The algorithm works in two phases:

  1. Row-wise XOR computation: For each row, we compute the prefix XOR to get cumulative XOR values from left to right.
  2. Column-wise XOR computation: For each column, we compute the prefix XOR to get the final coordinate values, which represent XOR of all elements in the rectangle from (0,0) to (i,j).
  3. Tracking k largest: We maintain a dictionary that keeps track of only the k largest values seen so far by removing the minimum when we exceed k values.

Step-by-Step Example

For the matrix [[5,2],[1,6]]:

# Original matrix
matrix = [[5, 2], [1, 6]]

# After row-wise prefix XOR
# Row 0: [5, 5^2=7]
# Row 1: [1, 1^6=7]
# Matrix becomes: [[5, 7], [1, 7]]

# After column-wise prefix XOR (coordinate values)
# (0,0): 5
# (0,1): 7  
# (1,0): 5^1 = 4
# (1,1): 7^7 = 0

coordinate_values = [5, 7, 4, 0]
print("All coordinate XOR values:", sorted(coordinate_values, reverse=True))
print("1st largest (k=1):", max(coordinate_values))
All coordinate XOR values: [7, 5, 4, 0]
1st largest (k=1): 7

Conclusion

This algorithm efficiently finds the k-th largest XOR coordinate value using prefix XOR computation and a sliding window approach to track only the k largest values. The time complexity is O(m×n) and space complexity is O(k) for the tracking dictionary.

Updated on: 2026-03-26T14:33:12+05:30

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements