VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-connected-group-making-a-large-island/

⇱ Maximum Connected group (Making A Large Island) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum Connected group (Making A Large Island)

Last Updated : 23 Jul, 2025

Given a binary matrix of size n x n, the task is to find the largest island after changing at most one cell from 0 to 1.

Note: An island is a 4-directionally (i.e, up, down, right, left) connected group of 1s.

Examples:

Input: grid = {{1 1},
{0 1}}
Output: 4
Explanation: By changing cell (2,1) ,we can obtain a connected group of 4 1's

Input: grid = {{1 0 1},
{1 0 1},
{1 0 1}}
Output: 7
Explanation: By changing cell (3,2) ,we can obtain a connected group of 7 1's

Input: grid = { { 1, 0, 1, 1, 0 },
{ 1, 0, 0, 1, 0 },
{ 0, 1, 1, 0, 1 },
{ 1, 0, 1, 0, 1 },
{ 0, 1, 0, 1, 0 } };
Output: 9

Naive approach: The basic way to solve the problem is as follows:

The idea to solve the problem is to use DFS. For each water cell (i.e, grid[i][j] = 0's ) maximise the result by summation of all unique neighbour's island size + 1 (i.e, we add 1 extra because we also have to convert the current water cell into land.

Follow the steps below to implement the above idea:

  • Iterate through each cell of the grid which represents water or 0's:
  • Count the size of the unique island in its neighbors (up, down, left, right)
  • Maintain a variable say result to track the largest island size found.
  • Return the result

Below is the implementation of the above approach:


Output
Largest island size: 9

Time Complexity: O(n4)
Auxiliary Space: O(n2)

Maximum Connected group using DFS:

The idea is to keep track of each island by some unique identity say key/name with its size. After this, go through each cell which is water (i.e, matrix[i][j] == 0) and add the sizes of unique neighbour's island into current size, finally maximize the currentsize in the result.

Follow the steps below to implement the above idea:

  • Initialize a result variable for storing the largest island.
  • Create an unordered map to store identity of each island with its sizes.
  • Explore each island and update the cell's value with a unique key and finally store key and size of island in map.
  • Iterate through each cell of the grid. which represents water or 0's:
  • Get sizes of neighboring islands from the map.
  • Calculate combined size of neighboring islands and the current cell.
  • Update the result with the maximum calculated size.

Below is the implementation of the above approach:


Output
Largest island size: 9

Time Complexity: O(n2)

  • The two nested loops iterate through all cells in the grid, resulting in O(n2)iterations.
  • Inside the loops, constant time operations are performed for DFS exploration and island size calculations.
  • The overall time complexity is O(n2), where n is the size of the grid.

Auxiliary Space: O(n2)

  • The visited matrix requires O(n2) space.
  • The unordered map stores island sizes, potentially up to O(n^2) distinct islands.
  • Other variables and data structures used occupy constant space.
  • The overall space complexity is O(n2).
Comment
Article Tags:
Article Tags: