Trains the technique from
LeetCode 781Rabbits in ForestThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Some of the moths in a jar were asked how many other moths share their own pattern, and replies holds the answers given, one per moth asked. Moths that were not asked said nothing.
A moth answering x belongs to a group of exactly x + 1 moths sharing one pattern, and moths in different groups never share a pattern.
Return the fewest moths the jar could hold.
Example 1
Two of the three moths can share a group of two, but the third needs a group of two of its own, so a fourth moth must be there unasked.
Example 2
Each answer names a different group size, so the four groups hold one, two, three and four moths.
Example 3
Groups of three are named. Three of the four moths fill one group and the fourth opens another, which needs two more moths to fill it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def num_rabbits(replies: list[int]) -> int:public int numRabbits(int[] replies)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.