Count Number of animals in a zoo from given number of head and legs
Last Updated :
27 Jul, 2022
Given the total number of Legs and Head of Rabbits and Pigeons. The task is to calculate the number of Rabbits and Pigeons.
Examples:
Input: Heads = 200, Legs = 540
Output: Rabbits = 70, Pigeons = 130
Input: Heads = 100, Legs = 300
Output: Rabbits = 50, Pigeons = 50
Let the total no. of Heads = 200 and Legs = 540.
Let the number of Head and Legs of Rabbits = X and that of Pigeons = Y.
so,
X + Y = 200 ..equation 1 (no. of the heads of a rabbit and a pigeon = total no. of heads)
(As both of them has 1 head)
4X + 2Y = 540 …equation 2 (no. of the legs of the rabbit and a pigeon = total no. of legs)
(Rabbit have 4 legs and pigeons have 2 legs)
Now, solving equation 1 and 2 we get,
4X = 540 – 2Y
4X = 540 – 2 * (200 – X)
4X = 540 – 400 + 2X
2X = 140 => X = 70
X = 70 and Y = 130.
hence, Rabbits = 70 and Pigeons = 130
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countRabbits( int Heads, int Legs)
{
int count = 0;
count = (Legs)-2 * (Heads);
count = count / 2;
return count;
}
int main()
{
int Heads = 100, Legs = 300;
int Rabbits = countRabbits(Heads, Legs);
cout << "Rabbits = " << Rabbits << endl;
cout << "Pigeons = " << Heads - Rabbits << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int countRabbits( int Heads,
int Legs)
{
int count = 0 ;
count = (Legs) - 2 * (Heads);
count = count / 2 ;
return count;
}
public static void main(String args[])
{
int Heads = 100 , Legs = 300 ;
int Rabbits = countRabbits(Heads, Legs);
System.out.println( "Rabbits = " +
Rabbits);
System.out.println( "Pigeons = " +
(Heads - Rabbits));
}
}
|
Python3
def countRabbits(Heads, Legs):
count = 0
count = (Legs) - 2 * (Heads)
count = count / 2
return count
if __name__ = = '__main__' :
Heads = 100
Legs = 300
Rabbits = countRabbits(Heads, Legs)
print ( "Rabbits =" , Rabbits)
print ( "Pigeons =" , Heads - Rabbits)
|
C#
using System;
class GFG
{
static int countRabbits( int Heads,
int Legs)
{
int count = 0;
count = (Legs) - 2 * (Heads);
count = count / 2;
return count;
}
public static void Main()
{
int Heads = 100, Legs = 300;
int Rabbits = countRabbits(Heads, Legs);
Console.WriteLine( "Rabbits = " +
Rabbits);
Console.WriteLine( "Pigeons = " +
(Heads - Rabbits));
}
}
|
PHP
<?php
function countRabbits( $Heads , $Legs )
{
$count = 0;
$count = ( $Legs ) - 2 * ( $Heads );
$count = (int) $count / 2;
return $count ;
}
$Heads = 100;
$Legs = 300;
$Rabbits = countRabbits( $Heads , $Legs );
echo "Rabbits = " , $Rabbits , "\n" ;
echo "Pigeons = " , $Heads -
$Rabbits , "\n" ;
?>
|
Javascript
<script>
function countRabbits(Heads, Legs)
{
var count = 0;
count = (Legs)-2 * (Heads);
count = count / 2;
return count;
}
var Heads = 100, Legs = 300;
var Rabbits = countRabbits(Heads, Legs);
document.write( "Rabbits = " + Rabbits + "<br>" );
document.write( "Pigeons = " + (Heads - Rabbits));
</script>
|
Output:
Rabbits = 50
Pigeons = 50
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...