Rabbits in Forest

MediumGreedyhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A forest contains an unknown number of rabbits, each with some color. You asked some of them the same question: "How many **other** rabbits share your color?" Their answers are stored in answers, where answers[i] is the reply of the i-th rabbit you asked. Every rabbit answers truthfully. Return the **minimum** possible number of rabbits in the forest that is consistent with all the answers (counting both the rabbits you asked and any you did not).

Examples

Input: answers = [1,1,2]
Output: 5
The two rabbits answering 1 can share a color group of size 2. The rabbit answering 2 is in a group of 3, two of which you did not ask. Total `2 + 3 = 5`.
Input: answers = [10,10,10]
Output: 11
All three can be in one color group of size 11.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems