Minimum number of rabbits that must be present in the forest
Last Updated :
18 Jan, 2023
There are some colored rabbits in a forest. Given an array arr[] of size N, such that arr[i] denotes the number of rabbits having same color as the ith rabbit, the task is to find the minimum number of rabbits that could be in the forest.
Examples:
Input: arr[] = {2, 2, 0}
Output: 4
Explanation: Considering the 1st and the 2nd rabbits to be of same color, eg. Blue, there should be 3 blue-colored rabbits. The third rabbit is the only rabbit of that color. Therefore, the minimum number of rabbits that could be present in the forest are = 3 + 1 = 4.
Input: arr[] = {10, 10, 10}
Output: 11
Explanation: Considering all the rabbits to be of the same color, the minimum number of rabbits present in forest are 10 + 1 = 11.
Approach: The approach to solving this problem is to find the number of groups of rabbits that have the same color and the number of rabbits in each group. Below are the steps:
- Initialize a variable count to store the number of rabbits in each group.
- Initialize a map and traverse the array having key as arr[i] and value as occurrences of arr[i] in the given array.
- Now, if y rabbits answered x, then:
- If (y%(x + 1)) is 0, then there must be (y / (x + 1)) groups of (x + 1) rabbits.
- If (y % (x + 1)) is non-zero, then there must be (y / (x + 1)) + 1 groups of (x + 1) rabbits.
- Add the product of the number of groups and the number of rabbits in each group to the variable count.
- After the above steps, the value of count gives the minimum number of rabbits in the forest.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minNumberOfRabbits( int answers[], int N)
{
map< int , int > Map;
for ( int a = 0; a < N; a++) {
Map[answers[a]]++;
}
int count = 0;
for ( auto a : Map) {
int x = a.first;
int y = a.second;
if (y % (x + 1) == 0)
count = count + (y / (x + 1)) * (x + 1);
else
count = count + ((y / (x + 1)) + 1) * (x + 1);
}
return count;
}
int main()
{
int arr[] = { 2, 2, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minNumberOfRabbits(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minNumberOfRabbits( int [] answers,
int N)
{
Map<Integer, Integer> map
= new HashMap<Integer, Integer>();
for ( int a : answers) {
map.put(a, map.getOrDefault(a, 0 ) + 1 );
}
int count = 0 ;
for ( int a : map.keySet()) {
int x = a;
int y = map.get(a);
if (y % (x + 1 ) == 0 ) {
count = count + (y / (x + 1 )) * (x + 1 );
}
else
count
= count + ((y / (x + 1 )) + 1 ) * (x + 1 );
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 0 };
int N = arr.length;
System.out.println(minNumberOfRabbits(arr, N));
}
}
|
Python3
def minNumberOfRabbits(answers, N):
Map = {}
for a in range (N):
if answers[a] in Map :
Map [answers[a]] + = 1
else :
Map [answers[a]] = 1
count = 0
for a in Map :
x = a
y = Map [a]
if (y % (x + 1 ) = = 0 ):
count = count + (y / / (x + 1 )) * (x + 1 )
else :
count = count + ((y / / (x + 1 )) + 1 ) * (x + 1 )
return count
arr = [ 2 , 2 , 0 ]
N = len (arr)
print (minNumberOfRabbits(arr, N))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
public static int minNumberOfRabbits( int [] answers,
int N)
{
Dictionary< int , int > map
= new Dictionary< int , int >();
for ( int a = 0; a < N; a++) {
if (map.ContainsKey(answers[a]))
map[answers[a]] += 1;
else
map.Add(answers[a], 1);
}
int count = 0;
for ( int a = 0; a < map.Count; a++) {
int x = map.Keys.ElementAt(a);
int y = map[x];
if (y % (x + 1) == 0) {
count = count + (y / (x + 1)) * (x + 1);
}
else
count
= count + ((y / (x + 1)) + 1) * (x + 1);
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 2, 2, 0 };
int N = arr.Length;
Console.WriteLine(minNumberOfRabbits(arr, N));
}
}
|
Javascript
<script>
function minNumberOfRabbits(answers, N)
{
var map = new Map();
for ( var a = 0; a < N; a++) {
if (map.has(answers[a]))
map.set(answers[a], map.get(answers[a])+1)
else
map.set(answers[a], 1)
}
var count = 0;
map.forEach((value, key) => {
var x = key;
var y = value;
if (y % (x + 1) == 0)
count = count + parseInt(y / (x + 1)) * (x + 1);
else
count = count + (parseInt(y / (x + 1)) + 1) * (x + 1);
});
return count;
}
var arr = [2, 2, 0];
var N = arr.length;
document.write( minNumberOfRabbits(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Another solution is to use HashMap of the counts and decrease the count every time the same answers[i] is met. If the same answers[i] occur again and the count is zero, reset the count with answers[i]+1 and add to the answer variable. Below are the steps:
- Initialize a variable count with zero.
- Use an unordered map mp.
- Traverse the array and do the following:
- if answers[i] is set to zero, set mp[answers[i]] = answers[i]+1 and count=count+answers[i]+1
- finally subtract the count from map, mp[answers[i]]–;
- return the cnt as answer.
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int minNumberOfRabbits( int answers[], int n)
{
int count = 0;
unordered_map< int , int > mp;
for ( int i = 0; i < n; ++i) {
if (mp[answers[i]] == 0) {
count += answers[i] + 1;
mp[answers[i]] = answers[i] + 1;
}
mp[answers[i]]--;
}
return count;
}
int main()
{
int arr[] = { 10, 10, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minNumberOfRabbits(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minNumberOfRabbits( int [] answers, int n)
{
int count = 0 ;
HashMap<Integer, Integer> mp = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < n; ++i) {
if (!mp.containsKey(answers[i]))
{
count += answers[i] + 1 ;
mp.put(answers[i],answers[i]+ 1 );
}
if (mp.containsKey(answers[i]))
mp.put(answers[i],mp.get(answers[i])- 1 );
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 10 , 10 , 0 };
int N = arr.length;
System.out.println(minNumberOfRabbits(arr, N));
}
}
|
Python3
def minNumberOfRabbits(answers, n):
count = 0
mp = {}
for i in range (n):
if (answers[i] not in mp):
count + = answers[i] + 1
mp[answers[i]] = answers[i] + 1
mp[answers[i]] - = 1
return count
if __name__ = = "__main__" :
arr = [ 10 , 10 , 0 ]
N = len (arr)
print (minNumberOfRabbits(arr, N))
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
public class GFG
{
public static int minNumberOfRabbits( int [] answers, int n)
{
var count = 0;
var mp = new Dictionary< int , int >();
for ( int i = 0; i < n; ++i)
{
if (!mp.ContainsKey(answers[i]))
{
count += answers[i] + 1;
mp[answers[i]] = answers[i] + 1;
}
if (mp.ContainsKey(answers[i]))
{
mp[answers[i]] = mp[answers[i]] - 1;
}
}
return count;
}
public static void Main(String[] args)
{
int [] arr = {10, 10, 0};
var N = arr.Length;
Console.WriteLine(GFG.minNumberOfRabbits(arr, N));
}
}
|
Javascript
<script>
class GFG
{
static minNumberOfRabbits(answers, n)
{
var count = 0;
var mp = new Map();
for ( var i=0; i < n; ++i)
{
if (!mp.has(answers[i]))
{
count += answers[i] + 1;
mp.set(answers[i],answers[i] + 1);
}
if (mp.has(answers[i]))
{
mp.set(answers[i],mp.get(answers[i]) - 1);
}
}
return count;
}
static main(args)
{
var arr = [10, 10, 0];
var N = arr.length;
document.write(GFG.minNumberOfRabbits(arr, N));
}
}
GFG.main([]);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...