Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Greedy
Rabbits In Forest

Given answers from rabbits about how many other rabbits share their color, compute the minimum possible total number of rabbits in the forest.

Acceptance 0%
Problem Statement

Rabbits in Forest

Each rabbit reports a number x, meaning: there are exactly x other rabbits in the forest with the same color as this rabbit.

You are given an array answers where answers[i] is the report from the i-th rabbit. Your task is to determine the minimum possible number of rabbits that could be in the forest while still being consistent with all reports.

A rabbit reporting x implies it belongs to a color group of size x + 1. Multiple rabbits may give the same report, and rabbits with the same report may belong to different color groups.

Return the minimum total number of rabbits that can satisfy all answers.

Input Format

Input

  • An integer array answers
    • answers[i] >= 0
    • each value represents a rabbit's report

Output Format

Output

  • Return a single integer: the minimum possible number of rabbits in the forest.

Constraints

Constraints

  • 1 <= answers.length
  • answers[i] are non-negative integers
  • Each report x corresponds to groups of size x + 1
Examples
Sample cases returned by the problem API.

Example 1

Input

answers = [1, 1, 2]

Output

5

Explanation

Two rabbits say there is 1 other rabbit of the same color, so they can form one group of size 2. One rabbit says there are 2 others of the same color, so it needs a group of size 3. Total minimum = 2 + 3 = 5.

Example 2

Input

answers = [10, 10, 10]

Output

11

Explanation

Each rabbit says there are 10 others of the same color, so all three can be placed in one color group of size 11. The minimum total is 11.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.