Skip to content

Practice Count Islands With Total Value Divisible by K

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3619
LeetCode Problem

Count Islands With Total Value Divisible by K

Problem Statement

You are given an m x n matrix grid and a positive integer k. An island is a group of positive integers (representing land) that are 4-directionally connected (horizontally or vertically). The total value of an island is the sum of the values of all cells in the island. Return the number of islands with a total value divisible by k.

Source: Count Islands With Total Value Divisible by K on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5
Output
2

# Example 2

Input
grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3
Output
6

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 10^5
0 <= grid[i][j] <= 10^6
1 <= k <= 10^6

Related LeetCode Problems

Practice more problems similar to Count Islands With Total Value Divisible by K.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.