Binary Matrix Rank (GF(2))

HardMatrixMathBit Manipulation

Description

Given a matrix of 0s and 1s, return its rank over the field GF(2) (i.e., arithmetic modulo 2 via Gaussian elimination using XOR).

Examples

Input:matrix = [[1,0],[0,1]]
Output:2
Explanation:

Identity has full rank 2.

Input:matrix = [[1,1],[1,1]]
Output:1
Explanation:

Rows linearly dependent.

Input:matrix = [[1,0,1],[0,1,1],[1,1,0]]
Output:2
Explanation:

Third row equals XOR of first two.

Constraints

  • 1 ≤ rows, cols ≤ 30
  • matrix[i][j] ∈ {0, 1}

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.