**Problem**: CHFIMPRS Problem - CodeChef

**My code**: CodeChef: Practical coding for everyone

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.

I have the same issue. Here’s my code.

It’d be great if anyone could share the approach or some hidden case I am missing, since editorial for this problem is not posted yet.

Please refer to line 112 in this submission:

1 Like

Can you elaborate a little?

You are printing the values different from the orignal matrix given.

Your solution is, in part, wrong because this condition `(m&1 && cnt != n)`

does not imply that there is no solution.

I later changed it to > n.

Submission 2 WA

Yes, but you haven’t changed your strategy of allocating numbers to the middle of rows

Use `v.back()`

instead of `v[v.size()-1]`

, it’s more clear and easier to write

I can’t see any glaring bugs in your code, have you tested it on small cases?

1 Like

yes . it shows correct output for those cases

Test it on more then, or write a stress

Oh, I see it, it *is* subtle.

My hint to you is: in a for-loop structured as `for (a; b; c)`

where `b`

is the condition to be checked, it’s re-checked every time. Go through your loops carefully and see how the operations done inside each loop might affect the conditions.

Error is present in Line Number 45. If you can’t find it by yourself then open the hint.

##
hint

o.size() is updating so the loop is not working as you expected. Try yourself for any valid matrix N = 11 and M = 7.

1 Like

Thank you very very much. got AC

Thanks a lot! All I had to do was if m == 1, make doubleFill not possible. CodeChef: Practical coding for everyone

Not doing this caused my program to fail in the following test case:

1

5 1

1

2

1

3

4

I was over-writing what I filled with fillD() with fillS().