Create a matrix of size m x n so that the neighbours of each cell have distinct values and the max value in this matrix is min

Pages: 12
Write your question here.

Example -
if n = 2 and m = 3:
1 1 2
2 3 3

Here every immediate neighbour of each cell has distinct values and the maximum value used in this matrix is 3.
Last edited on
I'm confused. In the first row the value 2 has a neighbor that is also 2. And in the second row, the value 3 has a neighbor that is also 3. What am I missing?
You have to make sure that the immediate neighbours of each cell are different.
It doesn't matter what the value is in the cells whose neighbours you are checking.
Hence, if we are checking for cell (x,y), then we have to make sure that the value in (x+1,y), (x-1, y), (x,y+1) and (x,y-1) are distinct. We don't care for the value in cell (x,y) is. We have to do this for all the cells in the matrix i.e for all i<=x<=n and 1<=x<=m.
So, are you wanting us to write the program for you? Not going to happen.

Write code that you think meets the requirement. Post that and we can give you advice if it doesn't do what you want.
@FurryGuy I am not looking for the code. I don't want the code. I am just unable to find the pattern in the rows and columns. I observed that the first row could be 1 1 2 2 1 1 .... always but its failing for n = 2 and m = 5 where the optimal choice would be
1 1 2 2 3
2 3 3 1 1
So you want an algorithm handed to you without showing you have tried to create a solution on your own.

If you have written some code, post it.
What are you saying?If I don't even know the algorithm to a question then how/why am I supposed to code it? How can I code an algorithm when I don't even know it. I am currently working through it on pen and paper.
@honeybuzz for n=2 m=3 you wrote
1 2 2
2 3 3
this is wrong as 1 has neighbours 2 and 2
@blackmamba fixed it.
Thanks for the explanation. I understand the problem now. I don't know why these posts were reported.

Hmm. It's an interesting problem.

One way to get unique neighbors is like so:

Row1: 1 4 7 1 4 7 1 4 7 ...
Row 2: 2 5 8 2 5 8 2 5 8 ...
Row 3: 3 6 9 3 6 9 3 6 9 ...

But will this give the minimum max value? I don't know.
its obviously not the most efficient way to solve the problem. The maximum value could be 4. Hence, we need to design an algorithm that can create the matrix using only 1,2,3 and 4.
@dhayden A much better way to do so would have been
1 1 2 2 1 1 2 2 1 1
2 3 3 4 4 3 3 4 4 2
2 4 1 1 2 2 1 1 3 2

But I can't see any pattern here
Last edited on
handle those cases differently.
thats where if else statements come into use
@blackmamba thanx
i just got 100 points
Oh, I thought the diagonal neighbors counted too.
@dhayden NVM I got it correct ;)
@shinjikagava there are already enough hints in the forum.
If u want direct solution to a problem then that is not going to happen.
And if u want to learn then hints already provided are enough.
Give it some time and work on it.
its not optimal
so should i binary search for min value of k for or fill with greedy heuristics?
is there some hidden observation that i am missing?
can someone give me some momentum so that i can code it.
i am getting 5 as max value of k.
Last edited on
Pages: 12