Dsa
Graphs Dfs
Easy
Consecutive Cell Groupings
Given a grid, where each cell contains a number representing an ID, write a function to count how many distinct groups of connected cells (cells with the same ID) exist. Cells are considered connected if they are adjacent either horizontally or vertically.
Input Format:
- The first line contains two integers, m and n, the dimensions of the grid.
- The next m lines contain n integers (representing IDs).
Output Format:
- Output the total count of distinct groups.
Example:
Input:
4 4
1 1 0 0
0 2 2 0
1 1 0 0
0 0 0 3
Output:
4
Key concepts
graphdfsgrouping
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now