# Find regions with most common region size in a given boolean matrix

• Difficulty Level : Easy
• Last Updated : 10 Sep, 2021

Given a boolean 2D array, arr[][] of size N*M where a group of connected 1s forms an island. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. The task is to find the position of the top left corner of all the regions with the most common region size.

Examples:

Input: arr[][] = {{0, 0, 1, 1, 0},
{1, 0, 1, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 1}}
Output: {1, 0}, {3, 4}
Explanation: There are 3 regions, two with length 1 and the other with length 4. So the length of most common region is 1 and the positions of those regions are {1, 0}, {3, 4}.

Input: arr[][] = {{0, 0, 1, 1, 0},
{0, 0, 1, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 1}}
Output: {0, 2}
Explanation: There are 2 regions, one with length 1 and the other with 4. Since both the regions have same frequency, both are the most common region. Hence, print position of any one of the regions.

Approach: The idea is based on the problem of finding the number of islands in Boolean 2D-matrix. The idea is to store the size of the regions along with their top-left corner position in a hashmap. And then iterate through the hashmap to find the most common region and print the required regions. Follow the steps below to solve the problem:

• Initialize a hashmap to store the size of the regions along with their top-left corner position.
• Maintain a visited array to keep track of all visited cells.
• Traverse the given 2D array, row-wise, and if the current element is ‘1’ and is not visited, perform DFS traversal from this node. After the traversal, store the size of the region in the map.
• After completing the above steps, iterate over the map to find the most common region and then print all the positions corresponding to the key in the map.

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` `#define ROW 4 ` `#define COL 5 ` ` `  `// Function to check if a given cell ` `// can be included in DFS ` `int` `isSafe(``int` `M[][COL], ``int` `row, ``int` `col, ` `           ``bool` `visited[][COL]) ` `{ ` `    ``return` `(row >= 0) ` ` ``&& (row < ROW) && (col >= 0) ` `           ``&& (col < COL) ` `           ``&& (M[row][col]  ` `&& !visited[row][col]); ` `} ` ` `  `// Utility function to do DFS for a 2D ` `// boolean matrix ` `void` `DFS(``int` `M[][COL], ``int` `row, ``int` `col, ` `         ``bool` `visited[][COL], ``int``& count) ` `{ ` `    ``// Initialize arrays to get row and column ` `    ``// numbers of 8 neighbours of a given cell ` `    ``static` `int` `rowNbr[]  ` `= { -1, -1, -1, 0, 0, 1, 1, 1 }; ` `    ``static` `int` `colNbr[]  ` `= { -1, 0, 1, -1, 1, -1, 0, 1 }; ` ` `  `    ``// Mark this cell as visited ` `    ``visited[row][col] = ``true``; ` ` `  `    ``// Recur for all connected neighbours ` `    ``for` `(``int` `k = 0; k < 8; ++k) { ` `        ``if` `(isSafe(M, row + rowNbr[k],  ` `col + colNbr[k], visited)) { ` ` `  `            ``// Increment region length by one ` `            ``count++; ` `            ``DFS(M, row + rowNbr[k], col + colNbr[k], ` `                ``visited, count); ` `        ``} ` `    ``} ` `} ` ` `  `// Function to find all the regions with most ` `// common length in the given boolean 2D matrix ` `void` `commonRegion(``int` `M[][COL]) ` `{ ` `    ``// Make a bool array to mark visited cells, ` `    ``// initially all cells are unvisited ` `    ``bool` `visited[ROW][COL]; ` `    ``memset``(visited, 0, ``sizeof``(visited)); ` ` `  `    ``// Map to store the size and the regions ` `    ``unordered_map<``int``, vector > > um; ` ` `  `    ``// Traverse through the ` `    ``// all cells of given matrix ` `    ``for` `(``int` `i = 0; i < ROW; ++i) { ` `        ``for` `(``int` `j = 0; j < COL; ++j) { ` ` `  `            ``// If the current cell is 1 and is ` `            ``// not visited ` `            ``if` `(M[i][j] && !visited[i][j]) { ` ` `  `                ``// Increment count by 1 ` `                ``int` `count = 1; ` ` `  `                ``// Perform DFS ` `                ``DFS(M, i, j, visited, count); ` `                ``um[count].push_back({ i, j }); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Store the most common region length ` `    ``int` `mostCommonRegion = 0; ` ` `  `    ``// Traverse the map to find the most common ` `    ``// region length ` `    ``for` `(``auto` `it = um.begin(); it != um.end(); it++) { ` `        ``int` `x = it->second.size(); ` `        ``mostCommonRegion = max(mostCommonRegion, x); ` `    ``} ` ` `  `    ``// Traverse the map to print the regions ` `    ``// having most common length ` `    ``for` `(``auto` `it = um.begin(); it != um.end(); it++) { ` ` `  `        ``int` `x = it->second.size(); ` `        ``if` `(mostCommonRegion == x) { ` ` `  `            ``// Print the top left position ` `// of the regions ` `            ``for` `(``int` `i = 0; i < it->second.size(); i++) { ` `                ``int` `x = it->second[i][0]; ` `                ``int` `y = it->second[i][1]; ` `                ``cout << ``"{"` `<< x << ``", "` `<< y << ``"}, "``; ` `            ``} ` `            ``break``; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Given Input ` `    ``int` `arr[][COL] = { { 0, 0, 1, 1, 0 }, ` `                       ``{ 1, 0, 1, 1, 0 }, ` `                       ``{ 0, 0, 0, 0, 0 }, ` `                       ``{ 0, 0, 0, 0, 1 } }; ` ` `  `    ``// Function call ` `    ``commonRegion(arr); ` ` `  `    ``return` `0; ` `}`

Output

`{1, 0}, {3, 4}, `

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

My Personal Notes arrow_drop_up
Recommended Articles
Page :