Help in FENCING

problem link.
why is this logic incorrect :
ans = (4 * Number of cells) - (2 * number of shared/common edges) ?
my submisison.

for each plant cell if the adjacent cell sharing a side is not a plant then we must put a fence in b/w them .

1 Like

Yes based on that,
we assume all cells are individual at first and would need 4 boundries for each.
next, now we no we don’t need boundaries where two plant cells meet (common/sharing edges), and that would decrease our boundary size by 2 (one less boundary for each cell among them).
example :-
xxx.
xxx.
....
....
here total number of cells is 6 and number of sharing edges is 7
ans is (4 * 6) - (7 * 2) = 10 (which is the expected answer).
i don’t see why this fails?

I cannot come up with a smaller tc but you can try this one .
your output is comming 32 but from accepted code it is comming 30.

1
7 7 8
6 2
2 4
2 3
3 1
3 6
4 3
7 5
2 7

1 Like

thankyou,
it was a very minor mistake.
i want to die real quick, how didn’t i see the whole return statement was missing in comparators.
AC

1 Like