Rabbits in Forest

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^th rabbit.

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

Example 1
Inputanswers = [1,1,2]
Output5
The two rabbits that answered "1" could both be the same color, while the rabbit that answered "2" must be a different color with 2 additional same-colored rabbits not in the array, so the minimum is 5.
Example 2
Inputanswers = [10,10,10]
Output11
All three rabbits that answered 10 can belong to one color group of size 11, so the minimum number of rabbits is 11.

Constraints

  • 1 <= answers.length <= 1000
  • 0 <= answers[i] < 1000

Asked at 9 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate