---
# System prepended metadata

title: 781. Rabbits in Forest
tags: [Greedy]

---

---
title: 781. Rabbits in Forest
tags: Greedy
description: share source code.
---

# 781. Rabbits in Forest

```java=
class Solution {
    public int numRabbits(int[] answers) {
   
        int cnt [] = new int [1001];
        for(int ans : answers){
            cnt [ans]++;
        }
        
        int res = 0;
        for(int i = 0; i < 1001; i++){
            if(cnt[i] != 0){
                //int num =  cnt[i]/(i + 1) + (cnt[i] % (i + 1) > 0 ? 1 : 0 );
                int num = 1 + (( cnt[i] - 1)/(i + 1));
                res += (i  + 1 )* num;
            }
                
            
        }
        
        return res;
    }
}
```

