Problem solution · C++

Count Islands with Total Value Divisible by K

Count Islands with Total Value Divisible by K: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
46 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Count Islands with Total Value Divisible by K, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 46 lines of C++ from the credited upstream file count-islands-with-total-value-divisible-by-k.cpp.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeCount Islands with Total Value Divisible by K · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(m * n)// Space: O(m + n) // bfs, flood fillclass Solution {public:    int countIslands(vector<vector<int>>& grid, int k) {        static const vector<pair<int, int>> DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};         const auto& bfs = [&](int i, int j) {            if (!grid[i][j]) {                return false;            }            int total = grid[i][j] % k;            grid[i][j] = 0;            vector<pair<int, int>> q = {{i, j}};            while (!empty(q)) {                vector<pair<int, int>> new_q;                for (const auto& [i, j] : q) {                    for (const auto& [di, dj] : DIRECTIONS) {                        const int ni = i + di, nj = j + dj;                        if (!(0 <= ni && ni < size(grid) && 0 <= nj && nj < size(grid[0]) && grid[ni][nj])) {                            continue;                        }                        total = (total + grid[ni][nj]) % k;                        grid[ni][nj] = 0;                        new_q.emplace_back(ni, nj);                    }                }                q = move(new_q);            }            return total == 0;        };         int result = 0;        for (int i = 0; i < size(grid); ++i) {            for (int j = 0; j < size(grid[0]); ++j) {                if (bfs(i, j)) {                    ++result;                }            }        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗