MTRXMOD- Please help me out. I can't figure out the problem in my code...

Can someone please look my solution and tell where the problem lies. I tried a lot but couldn’t figure out what is the problem with my code. Is it that I’m not giving lexicographically smallest answer?


I thought it has some issue with (-+) as I think, you just reverse the sign and just check with second row. Can you please explain why?
According to me, you have to search first non zero element (let’s say at ith index) reverse the sign of that element and then you have to check for ith column and i+1 to nth row (similar to what you have done but fixed 2nd column and 3rd row).
If anything unclear ping me back.

Happy Coding :slight_smile:


It’s clear that the first row/column is the actual array without sign. It could be seen that the first element is 0 and the 2nd element will be -ve for making it lexicographically smallest. Also, there could be only 2 such arrays which are exactly opposite in sign. Then, since the matrix is valid, so the 2nd column is also valid. So I traversed through the array and if mod(a[i]-a[2])!=arr[i,2], then surely mod(a[i]+a[2])==arr[i,2], so I changed the sign of the i th element instead of 2nd element so that it is smallest…

Your solution goes wrong for when a[2] = 0.
You need to find the smallest value of i for which b[1][i] != 0 and use that particular row (having the index as i) instead of row indexed 2.

I was making the same mistake, anushi’s reply to her answer in this editorial solved my doubt.



4 0
0 0 4 2
0 0 4 2
4 4 0 6
2 2 6 0

Your output: 0 0 -4 -2

Expected output: 0 0 -4 2

Please correct me if I made any mistakes.

Hope it helped. Credits to anushi