I recently received this in an interview and couldn't figure out how to solve it, I want to make sure I learn from this and know how to solve it for the future.
Question:
Let's define the beauty of a square matrix as the minimal positive integer which doesn't appear in this matrix. For example, if the square matrix 2 x 2 contains the numbers 1, 2, 4, and 6, then the beauty of this matrix will be equal to 3.
You are given a square matrix of positive integers called 'numbers', and an integer 'size' which evenly divides numbers.length. You task is to arrange the matrix elements in the following way:
- Split the matrix into non-overlapping 'size x size' submatrices;
- Arrange all the submatrices in ascending order of their beauty, then put them back into the matrix.
The order of the submatrices should be from left to right, from top to bottom. In case of a tie (If two submatrices have the same beauty) place them in relative order they were originally presented in the initial matrix. Here's the order of the submatrices in the case where there are 9 of them (ie. when numbers.length / size = 3):
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 10 | 7 9 10 | 8 9 10
----------------------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 7 | 4 6 7 | 5 6 7
8 9 10 | 8 9 10 | 8 9 10
----------------------------------
1 2 4 | 1 3 4 | 2 3 4
5 6 7 | 5 6 7 | 5 6 7
8 9 10 | 8 9 10 | 8 9 10
is (in terms of *beauty*):
9 | 8 | 7
---------
6 | 5 | 4
---------
3 | 2 | 1
And should be returned as:
2 3 4 | 1 3 4 | 1 2 4
5 6 7 | 5 6 7 | 5 6 7
8 9 10 | 8 9 10 | 8 9 10
----------------------------------
1 2 3 | 1 2 3 | 1 2 3
5 6 7 | 4 6 7 | 4 5 7
8 9 10 | 8 9 10 | 8 9 10
----------------------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
8 9 10 | 7 9 10 | 7 8 10
As a side note, I tend to be pretty bad at matrix manipulation type of questions in interviews so any advice or tips on how to improve would be appreciated!