Expand the Pond Problem

Getting TLE.
Please suggest changes.

Logic: for each 0, i searched the entire place to find connected 1’s and added 1 to the result. Done this for every 0 and maintained an ‘ans’ variable who will find out the maximum.

@Rhinorox run a DFS and make a Union of connected 1’s and give them unique I’d after that check for each zero that if it’s four side has different I’d one’s add the size of different union and store the maximum, this algorithm will run in O(nm) so it will not give tle, your algorithm is taking kn*m time where k is number of zero

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hey @Rhinorox
How can i help u
You already have the answer to ur query
See this for reference :https://ide.codingblocks.com/s/343335?_ga=2.200236910.1350203873.1600106484-1795954092.1597348154

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.