Number of Islands II Problem Statement

You have a 2D grid of dimensions 'N' rows by 'M' columns, initially filled with water. You are given 'Q' queries, where each query contains two integers 'X' and 'Y'. During each query, convert the water at position ('X', 'Y') into land. The task is to determine the number of islands present on the grid after each query.

An island is defined as a group of lands surrounded by water horizontally, vertically, or diagonally.

Input:

The first line contains an integer 'T' denoting the number of test cases.  

The first input line of each test case contains two integers 'N' and 'M', separated by a space, which represent the dimensions of the grid.

The second line of each test case includes an integer 'Q', indicating the number of queries.

The next 'Q' lines consist of two integers 'X' and 'Y', separated by space, representing the coordinates to turn into land.

Output:

For each test case, output a single integer for each query, indicating the number of islands present after the respective query operation.

Provide the output of each test case on a separate line.
Example:
Input: 
1 
3 3 
3 
0 0 
0 1 
1 2 

Output: 
1 
1 
2

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 1000
  • 1 <= M <= 1000
  • 1 <= Q <= 100000
  • 0 <= X < N
  • 0 <= Y < M
  • Time limit: 1 sec
Note:
There is no need to print anything; focus on implementing the function as required.
Be the first one to answer
Add answer anonymously...
MakeMyTrip Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter