Connect 2 islands

What will be logic for this

hi @kumarakash121005,
you need to connect 2 island group with minimum distance since the weights are equal on all sides (i.e 1) you can use bfs from first island group till you reach other group as soon as you get the first member from the second island group you should stop and return the no of levels of bfs you did

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.

i am not able to understand how the logic is going

hi @kumarakash121005, you can reafer the discuss secion of https://leetcode.com/problems/shortest-bridge/ its same question with same approach

in this discussion there are codes and i was seen it earlier and no proper logic

@kumarakash121005,
logic is same as i told above from one island group you need to go to another ,
suppose the size of island would have been 1 only (i.e 1 cell only) then the question becomes shortest path between 2 cell right? so u can use BFS directly.
But here the island group contains more than 1 cell so u r not able to visualize.
Consider whole thing as 1 group and first insert all the members in the queue ( for size 1 u just insert 1 cell but here insert all) now start doing BFS as soon as you reach the other group stop and return the steps you took

same has been implemented here https://ide.codingblocks.com/s/661183 ive commented the above logic here also do a dry run you’ll get it

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.