Problem

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

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

Examples

Example 1:

1
2
3
4
5
6
7
8
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.

Example 2:

1
2
Input: answers = [10,10,10]
Output: 11

Constraints:

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

Solution

Each rabbit in the forest can reply with a number, answers[i], which represents how many rabbits (including itself) have the same colour as it. If several rabbits give the same answer, they might belong to the same group. To determine the minimum number of rabbits in the forest, we can group the responses intelligently based on how many rabbits belong to the same colour group.

Method 1 - Greedy

Here is the approach:

  1. Group Responses:
    • The answer k from one rabbit means there can be at most k+1 rabbits in that colour group (including the rabbit itself).
    • For example, if a rabbit says 2, it means there can be 2+1=3 rabbits in its colour group.
    • Count how many rabbits give each response using a frequency dictionary.
  2. Calculate Minimum Rabbits:
    • For each unique response k, determine the minimum number of rabbits by grouping.
    • If there are count rabbits with a response k, and each group can accommodate up to k+1 rabbits, use (count + k) // (k + 1) groups to compute the total rabbits needed for that response.
  3. Edge Cases:
    • If there are no responses (empty array), return 0.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {

    public int numRabbits(int[] answers) {
        HashMap<Integer, Integer> freq = new HashMap<>();
        for (int ans : answers) {
            freq.put(ans, freq.getOrDefault(ans, 0) + 1);
        }

        int totalRabbits = 0;
        for (int k : freq.keySet()) {
            int count = freq.get(k);
            int groupSize = k + 1;
            int groups = (count + k) / groupSize;
            totalRabbits += groups * groupSize;
        }

        return totalRabbits;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        freq = {}
        for ans in answers:
            freq[ans] = freq.get(ans, 0) + 1

        total_rabbits = 0
        for k, count in freq.items():
            group_size = k + 1
            groups = (count + k) // group_size
            total_rabbits += groups * group_size

        return total_rabbits

Complexity

  • ⏰ Time complexity: O(n)
    • Creating a frequency dictionary takes O(n).
    • Iterating through the dictionary and calculating groups is O(u), where u is the number of unique responses (usually small compared to n).
    • Overall complexity: O(n).
  • 🧺 Space complexity: O(u) for storing frequency data.