Skip to content

Conversation

@Daredevil124
Copy link

Given a 2D grid where each cell contains an integer value, the task is to count the number of islands (connected components of non-zero cells) such that the sum of all cell values in an island is divisible by k.

Approach

  • Traverse the entire grid using nested loops.
  • For each unvisited cell with a positive value, perform a Depth-First Search (DFS) to explore the entire connected component (island).
  • During DFS, accumulate the sum of all cell values in the island and mark visited cells.
  • After the DFS completes for one island, check if the total sum is divisible by k.
  • If yes, increment the island count.
  • Return the final count after processing the whole grid.

Complexity Analysis

  • Time Complexity: O(m × n), where m and n are the grid dimensions — each cell is visited once.
  • Space Complexity: O(m × n) for the visited array and recursion stack.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo

@SjxSubham SjxSubham changed the title Added the Leetcode problem number 3619 3495 Minimum Operations To Make Array Elements Zero.cpp Oct 6, 2025
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 6, 2025
@SjxSubham
Copy link
Owner

@Daredevil124

Star the repo⭐

Happy HAcktober... keep contributing....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants