**Problem**: https://www.codechef.com/problems/CHFIMPRS

**My code**: https://www.codechef.com/viewsolution/33319047

I’ve kept the code as readable as possible, in case you don’t understand anything feel free to ask.

Basically what I’m doing is, calculating the frequencies of each integer in the matrix. If the number of columns is even and anyone of the frequencies is odd, then its not possible to impress chefina. Else I proceed to fill the matrix.

For example if the matrix has numbers 1, 2, 3 with frequencies 5, 2, 2 respectively then this is how I fill it:

0 0 0

0 0 0

0 0 0

1 0 1

0 0 0

0 0 0

1 0 1

1 0 1

0 0 0

1 1 1

1 0 1

0 0 0

1 1 1

1 0 1

2 0 2

1 1 1

1 3 1

2 3 2

FillD() is filling a number two times in a row. FillS() is filling a single number in the middle of that row.

Iterating through the set of integers present in the matrix, I try to fill it in the matrix . I try to place them two at a time first. So at the end of that process, either I can’t fill the matrix such that the rows are a palindrome or I end up with the remaining frequency of that integer to be 0 or 1.

If its 1, I try to use the FillS() operation to fill it. If its 0, then I can proceed to the next integer.