Let's consider you have given a 2D array with values (0 or 1).
Count the total number of group of adjacent 1's in a given array.
Example 1:
1, 0, 0
0, 0, 0
0, 0, 1
Answer: 2
Explanation: In the above example, single 1's block is also considered as one group.
Example 2:
1, 1, 0, 1, 1, 0
0, 1, 0, 0, 0, 1
0, 1, 0, 1, 1, 0
0, 1, 1, 0, 0, 0
Answer: 1
Explanation: In the above example, a group of 1's block is adjacent with at least one 1's block.
My solution: https://play.golang.org/p/nyw4lm6yrQ1
But it looks like, the time complexity is O(n^2)