> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/algorithm/tree/problems-set-1.md).

# Problems - Set 1

## Easy

## Medium

### Pond Sizes

We are given a **2D grid of integers** that represents a plot of land. Each cell in the grid has a number:

* If the number is `0`, it means **water**.
* Any number greater than `0` means **land**, with the value representing the height above sea level.

**Find all the ponds** in the matrix and return their **sizes**.

A **pond** is a group of water cells (`0`s) that are **connected** to each other. Two water cells are considered **connected** if they touch **vertically**, **horizontally**, or **diagonally**.

For each such group (pond), we want to know how many `0`s are in that group — that's the **size** of the pond.

A 2D array like this:

```
0 2 1 0
0 1 0 1
1 1 0 1
0 1 0 1
```

A list of integers representing the sizes of the ponds.\
For the input above, the expected result is:

```
[2, 1, 4]
```

Let's mark all water (`0`) cells and group them if they are connected:

There are **three ponds**:

1. **Pond A**: top-left corner `0`s → size 2
2. **Pond B**: single `0` at top-right corner → size 1
3. **Pond C**: group of 4 `0`s in the center-bottom → size 4

We can solve the **Pond Sizes** problem using **Depth-First Search (DFS)** or **Breadth-First Search (BFS)** to find all connected `0`s in the matrix, including diagonally. Each group of connected water cells is considered a pond, and we compute the size (number of cells) of each.

```java
import java.util.*;

public class PondSizes {

    public static List<Integer> computePondSizes(int[][] land) {
        List<Integer> pondSizes = new ArrayList<>();
        int rows = land.length;
        int cols = land[0].length;
        boolean[][] visited = new boolean[rows][cols];

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                if (land[r][c] == 0 && !visited[r][c]) {
                    int size = dfs(land, visited, r, c);
                    pondSizes.add(size);
                }
            }
        }

        return pondSizes;
    }

    private static int dfs(int[][] land, boolean[][] visited, int r, int c) {
        if (r < 0 || c < 0 || r >= land.length || c >= land[0].length) return 0;
        if (land[r][c] != 0 || visited[r][c]) return 0;

        visited[r][c] = true;
        int size = 1;

        // 8 directions: vertical, horizontal, diagonal
        int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
        int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};

        for (int d = 0; d < 8; d++) {
            size += dfs(land, visited, r + dx[d], c + dy[d]);
        }

        return size;
    }

    public static void main(String[] args) {
        int[][] land = {
            {0, 2, 1, 0},
            {0, 1, 0, 1},
            {1, 1, 0, 1},
            {0, 1, 0, 1}
        };

        List<Integer> result = computePondSizes(land);
        Collections.sort(result); // optional, for ordered output
        System.out.println("Pond sizes: " + result);  // Expected: [1, 2, 4]
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/algorithm/tree/problems-set-1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
