Find the number of Chicks in a Zoo at Nth day
Given that a zoo has a single chick. A chick gives birth to 2 chicks every day and the life expectancy of a chick is 6 days. The task is to find the number of chicks on the Nth day.
Examples:
Input: N = 3
Output: 9
First day: 1 chick
Second day: 1 + 2 = 3
Third day: 3 + 6 = 9
Input: N = 12
Output: 173988
Simple approach: It is given that the life expectancy of a chick is 6 days, so no chick dies till the sixth day. The everyday population of the current day will be 3 times of the previous day. One more thing is to note that the chick born on ith day is not counted on that day, it will be counted in the next day and the changes begin from the seventh day. So main calculation starts from the seventh day onwards.
On Seventh Day: Chicks from the 1st day die so based on manual calculation it will be 726.
On Eighth Day: Two newborn chicks born on (8-6)th i.e 2nd day dies. This will affect the current population by 2/3. This population needs to be get deducted from the previous day’s population because today i.e 8th day more newborns will be born, so we cannot deduct directly from today’s population. This will then be multiplied by three times because of newborns born on that day.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll getChicks( int n)
{
int size = max(n, 7);
ll dp[size];
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= 6; i++) {
dp[i] = dp[i - 1] * 3;
}
dp[7] = 726;
for ( int i = 8; i <= n; i++) {
dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3;
}
return dp[n];
}
int main()
{
int n = 7;
cout << getChicks(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static long getChicks( int n)
{
int size = Math.max(n, 7 );
long []dp = new long [size+ 1 ];
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= 6 ; i++) {
dp[i] = dp[i - 1 ] * 3 ;
}
dp[ 7 ] = 726 ;
for ( int i = 8 ; i <= n; i++) {
dp[i] = (dp[i - 1 ] - ( 2 * dp[i - 6 ] / 3 )) * 3 ;
}
return dp[n];
}
public static void main(String[] args) {
int n = 7 ;
System.out.println(getChicks(n));
}
}
|
Python3
def getChicks(n):
size = max (n, 7 );
dp = [ 0 ] * (size + 1 );
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
for i in range ( 2 , 7 ):
dp[i] = dp[i - 1 ] * 3 ;
dp[ 7 ] = 726 ;
for i in range ( 8 ,n + 1 ):
dp[i] = (dp[i - 1 ] - ( 2 * dp[i - 6 ] / / 3 )) * 3 ;
return dp[n];
n = 7 ;
print (getChicks(n));
|
C#
using System;
class GFG
{
static long getChicks( int n)
{
int size = Math.Max(n, 7);
long []dp = new long [size+1];
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= 6; i++)
{
dp[i] = dp[i - 1] * 3;
}
dp[7] = 726;
for ( int i = 8; i <= n; i++)
{
dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3;
}
return dp[n];
}
static public void Main ()
{
int n = 7;
Console.WriteLine(getChicks(n));
}
}
|
Javascript
<script>
function getChicks(n)
{
let size = Math.max(n, 7);
let dp = new Array(size+1);
dp.fill(0);
dp[0] = 0;
dp[1] = 1;
for (let i = 2; i <= 6; i++)
{
dp[i] = dp[i - 1] * 3;
}
dp[7] = 726;
for (let i = 8; i <= n; i++)
{
dp[i] = (dp[i - 1] -
(2 * parseInt(dp[i - 6] / 3, 10))) * 3;
}
return dp[n];
}
let n = 7;
document.write(getChicks(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(max(n, 7)), where n is the given input.
An efficient approach:
The problem would be more easy if there was no life expectancy of chicks. But here we have to take care of that also. We will maintain an array to keep count of how many chicks expires in each day.
We will maintain an array to keep count of how many chicks expire each day. We can take this array of size 42(minimum) as the maximum length of N is 35 and we have to take more extra 6 days as the life expectancy of a chick is 6 days.
Follow the steps below to implement the above approach:
- Initially, maintain a variable called cnt to keep the count of live chicks. Initialize cnt=1 as there was only 1 chick on day 1.
- Declare an array named expires[] of size greater than or equal to 42.
- assign expires[0]=6 as first chick will expire on day 6.
- Run a loop from i=1 to n
- In each iteration, reduce cnt by expires[i] as those chicks will expire on that day.
- Add 2*cnt to expires[i+6] as 2*cnt will expire on (i+6)th day.
- Add 2*cnt to cnt as 2*cnt chicks are born on each day
- At the end, return cnt after the loop ends.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
long long int NoOfChicks( int n)
{
long long cnt = 1ll;
vector< long long > expires(50, 0);
expires[6] = 1;
for ( int i = 1; i < n; i++) {
cnt -= expires[i];
expires[i + 6] += 2 * cnt;
cnt += (2 * cnt);
}
return cnt;
}
int main()
{
int n = 7;
cout << NoOfChicks(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static long NoOfChicks( int N)
{
long temp = 1 ;
long ans = 1 ;
long arr[] = new long [N];
boolean flag = false ;
arr[ 0 ] = ans;
int ind = 0 ;
for ( int i = 1 ; i < N; i++) {
temp++;
if (temp % 7 == 0 || flag) {
flag = true ;
ans -= arr[ind++];
}
long val = ans * 2 ;
arr[i] = val;
ans += val;
}
return ans;
}
public static void main(String[] args)
{
int n = 7 ;
System.out.println(NoOfChicks(n));
}
}
|
Python3
def NoOfChicks(N):
arr = [ 0 ] * (N + 1 )
actual = [ 0 ] * (N + 1 )
arr[ 1 ] = 1
actual[ 1 ] = 1
for i in range ( 2 , N + 1 ):
curr = i - 6
add = arr[i - 1 ]
actual[i] = add * 2
arr[i] = add * 2
arr[i] + = arr[i - 1 ]
if curr > = 1 :
arr[i] - = 3 * actual[curr]
actual[i] - = 2 * actual[curr]
return arr[N]
n = 7
print (NoOfChicks(n))
|
C#
using System;
public class GFG {
public static long NoOfChicks( int N)
{
long temp = 1;
long ans = 1;
long [] arr = new long [N];
bool flag = false ;
arr[0] = ans;
int ind = 0;
for ( int i = 1; i < N; i++) {
temp++;
if (temp % 7 == 0 || flag) {
flag = true ;
ans -= arr[ind++];
}
long val = ans * 2;
arr[i] = val;
ans += val;
}
return ans;
}
static public void Main()
{
int n = 7;
Console.WriteLine(NoOfChicks(n));
}
}
|
Javascript
function NoOfChicks(N)
{
let temp = 1;
let ans = 1;
let arr = [];
let flag = false ;
arr[0] = ans;
let ind = 0;
for (let i = 1; i < N; i++) {
temp++;
if (temp % 7 == 0 || flag) {
flag = true ;
ans -= arr[ind++];
}
let val = ans * 2;
arr[i] = val;
ans += val;
}
return ans;
}
let n = 7;
console.log(NoOfChicks(n));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
An constant space O(1) approach:
As you know the life expectancy of chicks is 6 days, so in order to get the number of chicks alive today (lets say ith day), you have to subtract the newBornChick at i – 6th day from ith day and double it, described above. This method of creating vector of size 50 can be reduced by only size 7 or constant space by simply creating a findIndex() method, which takes the ith element as input and return an index by doing a modulo division with 6. if i = 7 then findIndex() will return 1 and we will store the value (newly born chicks on that day) to newBornChicks[i], and if the method return 0, we will store the value at index 6(means this method will return 6).
Here, we will maintain a vector/array of size 7 which will store the number of newly born chicks every day.
Follow the steps below to implement the above idea:
- Declare an array newBornChicks[] and totalChicks[] of size 7
- Initialise newBornChicks[1] and totalChicks = 1; giving the initial values.
- Run a for loop from 1 to N (inclusive).
- check, if i is less than equal 6,
- If true, store double of totalChicks at newBornChicks[i].
- update totalChicks with totalChicks + newBornChicks[i].
- if i is greater than 6,
- first find the index by performing modulus division operation
- subtract the number of dead chicks which are born i-6th day, totalChicks -= newBornChicks[index].
- now push the today’s new born chicks to at that index.
- and update the total chicks by totalChicks with today’s newBornChicks
- Return the totalChicks.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
class GFG
{
int findIndex ( int k)
{
if (k%6 == 0)
{
return 6;
}
else
{
return (k%6);
}
}
public :
long long int NoOfChicks( int N)
{
if (N == 1)
{
return 1;
}
long long int newBorn[7], totalChicks;
newBorn[1] = totalChicks = 1;
for ( int i = 2; i <= N; i++)
{
if (i <= 6)
{
newBorn[i] = totalChicks<<1;
totalChicks += newBorn[i];
}
else
{
int index = findIndex(i);
totalChicks -= newBorn[index];
newBorn[index] = totalChicks<<1;
totalChicks += newBorn[index];
}
}
return totalChicks;
}
};
int main()
{
int N = 7;
GFG obj;
cout << obj.NoOfChicks(N) << "\n" ;
return 0;
}
|
Java
public class GFG
{
private static int findIndex ( int k)
{
if (k% 6 == 0 )
{
return 6 ;
}
else
{
return (k% 6 );
}
}
public static long NoOfChicks( int N)
{
if (N == 1 )
{
return 1 ;
}
long []newBorn = new long [ 7 ];
long totalChicks;
newBorn[ 1 ] = totalChicks = 1 ;
for ( int i = 2 ; i <= N; i++)
{
if (i <= 6 )
{
newBorn[i] = totalChicks<< 1 ;
totalChicks += newBorn[i];
}
else
{
int index = findIndex(i);
totalChicks -= newBorn[index];
newBorn[index] = totalChicks<< 1 ;
totalChicks += newBorn[index];
}
}
return totalChicks;
}
public static void main(String[] args)
{
int n = 7 ;
System.out.println(NoOfChicks(n));
}
}
|
Python
def findIndex(k):
if (k % 6 = = 0 ):
return 6
else :
return (k % 6 )
def NoOfChicks(N):
if (N = = 1 ):
return 1
newBorn = []
totalChicks = 0
for i in range ( 0 , 7 ):
newBorn.append( 0 )
newBorn[ 1 ] = totalChicks = 1
for i in range ( 2 , N + 1 ):
if (i < = 6 ):
newBorn[i] = totalChicks << 1
totalChicks + = newBorn[i]
else :
index = findIndex(i)
totalChicks - = newBorn[index]
newBorn[index] = totalChicks << 1
totalChicks + = newBorn[index]
return totalChicks
N = 7
print (NoOfChicks(N))
|
C#
using System;
public class GFG
{
private static int findIndex( int k)
{
if (k % 6 == 0)
{
return 6;
}
else
{
return (k % 6);
}
}
public static long NoOfChicks( int N)
{
if (N == 1)
{
return 1;
}
long [] newBorn = new long [7];
long totalChicks;
newBorn[1] = totalChicks = 1;
for ( int i = 2; i <= N; i++)
{
if (i <= 6)
{
newBorn[i] = totalChicks << 1;
totalChicks += newBorn[i];
}
else
{
var index = GFG.findIndex(i);
totalChicks -= newBorn[index];
newBorn[index] = totalChicks << 1;
totalChicks += newBorn[index];
}
}
return totalChicks;
}
public static void Main(String[] args)
{
var n = 7;
Console.WriteLine(GFG.NoOfChicks(n));
}
}
|
Javascript
function findIndex (k)
{
if (k%6 == 0)
{
return 6;
}
else
{
return (k%6);
}
}
function NoOfChicks( N)
{
if (N == 1)
{
return 1;
}
let newBorn =[], totalChicks;
for (let i = 0; i < 7; i++)
newBorn.push(0);
newBorn[1] = totalChicks = 1;
for (let i = 2; i <= N; i++)
{
if (i <= 6)
{
newBorn[i] = totalChicks<<1;
totalChicks += newBorn[i];
}
else
{
let index = findIndex(i);
totalChicks -= newBorn[index];
newBorn[index] = totalChicks<<1;
totalChicks += newBorn[index];
}
}
return totalChicks;
}
let N = 7;
console.log(NoOfChicks(N));
|
Time Complexity: O(N).
Auxiliary Space: O(1).
Last Updated :
02 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...