Given an integer N which is a multiple of 4, the task is to find an N x N grid for which the bitwise xor of every row and column is same.
Input: N = 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Input: N = 8
0 1 2 3 16 17 18 19
4 5 6 7 20 21 22 23
8 9 10 11 24 25 26 27
12 13 14 15 28 29 30 31
32 33 34 35 48 49 50 51
36 37 38 39 52 53 54 55
40 41 42 43 56 57 58 59
44 45 46 47 60 61 62 63
Approach: To solve this problem lets fix the xor of every row and column to 0 since xor of 4 consecutive numbers starting from 0 is 0. Here is an example of a 4 x 4 matrix:
0 ^ 1 ^ 2 ^ 3 = 0
4 ^ 5 ^ 6 ^ 7 = 0
8 ^ 9 ^ 10 ^ 11 = 0
12 ^ 13 ^ 14 ^ 15 = 0
and so on.
If you notice in the above example, the xor of every row and column is 0. Now we need to place the numbers in such a way that the xor of each row and column is 0.So we can divide our N x N matrix into smaller 4 x 4 matrices with N / 4 rows and columns and fill the cells in a way that the xor of every row and column is 0.
Below is the implementation of the above approach:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Time Complexity: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Find a Square Matrix such that sum of elements in every row and column is K
- Count number of islands where every island is row-wise and column-wise separated
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Replace every matrix element with maximum of GCD of row or column
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find the element at R'th row and C'th column in given a 2D pattern
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Find the original matrix when largest element in a row and a column are given
- Find if a binary matrix exists with given row and column sums
- Find all matrix elements which are minimum in their row and maximum in their column
- Place N^2 numbers in matrix such that every row has an equal sum
- Search in a Row-wise and Column-wise Sorted 2D Array using Divide and Conquer algorithm
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Search in a row wise and column wise sorted matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.