> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Rabbits in Forest Python Solution with Tests

> Tested Python solution for LeetCode 781 with 47 pytest cases. Generate a practice environment with lcpy.

LeetCode 781, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/rabbits-in-forest/description/).

Generate this problem as a practice environment: tested reference solution, 47 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 781   # by problem number
lcpy gen -s rabbits_in_forest   # by problem name
```

## Problem

There is a forest with an unknown number of rabbits. We asked n rabbits **"How many other rabbits have the same color as you?"** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the `i<sup>th</sup>` rabbit.

Given the array `answers`, return *the minimum number of rabbits that could be in the forest*.

### Examples

```
Input: answers = [1,1,2]
Output: 5
Explanation:
The two rabbits that answered "1" could both be the same color, say red.
The rabbit that answered "2" can't be red or the answers would be inconsistent.
Say the rabbit that answered "2" was blue.
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
```

```
Input: answers = [10,10,10]
Output: 11
```

### Constraints

* `1 <= answers.length <= 1000`
* `0 <= answers[i] < 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rabbits_in_forest/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rabbits_in_forest/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(k)
    def num_rabbits(self, answers: list[int]) -> int:
        total = 0
        for answer, count in Counter(answers).items():
            group_size = answer + 1
            total += -(-count // group_size) * group_size
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(k) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.