Given that a zoo has a single chick. A chick gives birth to 2 chicks everyday and the life expectancy of a chick is 6 days. The task is to find the number of chicks on the **N ^{th}** 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. Every day population of current day will be 3 time of 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 seventh day. So main calculation starts from seventh day onwards.

**On Seventh Day:** Chicks from the 1st day die so based on manual calculation it will be 726.

**On Eigth Day:** Two new born 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 population because today i.e 8th day more newborns will we 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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to return the number ` `// of chicks on the nth day ` `ll getChicks(` `int` `n) ` `{ ` ` ` ` ` `// Size of dp[] has to be ` ` ` `// at least 6 (1-based indexing) ` ` ` `int` `size = max(n, 7); ` ` ` `ll dp[size]; ` ` ` ` ` `dp[0] = 0; ` ` ` `dp[1] = 1; ` ` ` ` ` `// Every day current population ` ` ` `// will be three times of the previous day ` ` ` `for` `(` `int` `i = 2; i <= 6; i++) { ` ` ` `dp[i] = dp[i - 1] * 3; ` ` ` `} ` ` ` ` ` `// Manually calculated value ` ` ` `dp[7] = 726; ` ` ` ` ` `// From 8th day onwards ` ` ` `for` `(` `int` `i = 8; i <= n; i++) { ` ` ` ` ` `// Chick population decreases by 2/3 everyday. ` ` ` `// For 8th day on [i-6] i.e 2nd day population ` ` ` `// was 3 and so 2 new born die on the 6th day ` ` ` `// and so on for the upcoming days ` ` ` `dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3; ` ` ` `} ` ` ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << getChicks(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to return the number ` `// of chicks on the nth day ` `static` `long` `getChicks(` `int` `n) ` `{ ` ` ` ` ` `// Size of dp[] has to be ` ` ` `// at least 6 (1-based indexing) ` ` ` `int` `size = Math.max(n, ` `7` `); ` ` ` `long` `[]dp = ` `new` `long` `[size]; ` ` ` ` ` `dp[` `0` `] = ` `0` `; ` ` ` `dp[` `1` `] = ` `1` `; ` ` ` ` ` `// Every day current population ` ` ` `// will be three times of the previous day ` ` ` `for` `(` `int` `i = ` `2` `; i < ` `6` `; i++) { ` ` ` `dp[i] = dp[i - ` `1` `] * ` `3` `; ` ` ` `} ` ` ` ` ` `// Manually calculated value ` ` ` `dp[` `6` `] = ` `726` `; ` ` ` ` ` `// From 8th day onwards ` ` ` `for` `(` `int` `i = ` `8` `; i <= n; i++) { ` ` ` ` ` `// Chick population decreases by 2/3 everyday. ` ` ` `// For 8th day on [i-6] i.e 2nd day population ` ` ` `// was 3 and so 2 new born die on the 6th day ` ` ` `// and so on for the upcoming days ` ` ` `dp[i] = (dp[i - ` `1` `] - (` `2` `* dp[i - ` `6` `] / ` `3` `)) * ` `3` `; ` ` ` `} ` ` ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) { ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(getChicks(n)); ` ` ` `} ` `} ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python implementation of the approach ` ` ` `# Function to return the number ` `# of chicks on the nth day ` `def` `getChicks(n): ` ` ` ` ` `# Size of dp[] has to be ` ` ` `# at least 6 (1-based indexing) ` ` ` `size ` `=` `max` `(n, ` `7` `); ` ` ` `dp ` `=` `[` `0` `]` `*` `size; ` ` ` ` ` `dp[` `0` `] ` `=` `0` `; ` ` ` `dp[` `1` `] ` `=` `1` `; ` ` ` ` ` `# Every day current population ` ` ` `# will be three times of the previous day ` ` ` `for` `i ` `in` `range` `(` `2` `,` `7` `): ` ` ` `dp[i] ` `=` `dp[i ` `-` `1` `] ` `*` `3` `; ` ` ` ` ` `# Manually calculated value ` ` ` `dp[` `6` `] ` `=` `726` `; ` ` ` ` ` `# From 8th day onwards ` ` ` `for` `i ` `in` `range` `(` `8` `,n` `+` `1` `): ` ` ` ` ` `# Chick population decreases by 2/3 everyday. ` ` ` `# For 8th day on [i-6] i.e 2nd day population ` ` ` `# was 3 and so 2 new born die on the 6th day ` ` ` `# and so on for the upcoming days ` ` ` `dp[i] ` `=` `(dp[i ` `-` `1` `] ` `-` `(` `2` `*` `dp[i ` `-` `6` `] ` `/` `/` `3` `)) ` `*` `3` `; ` ` ` ` ` `return` `dp[n]; ` ` ` `# Driver code ` `n ` `=` `3` `; ` ` ` `print` `(getChicks(n)); ` ` ` `# This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number ` `// of chicks on the nth day ` `static` `long` `getChicks(` `int` `n) ` `{ ` ` ` ` ` `// Size of dp[] has to be ` ` ` `// at least 6 (1-based indexing) ` ` ` `int` `size = Math.Max(n, 7); ` ` ` `long` `[]dp = ` `new` `long` `[size]; ` ` ` ` ` `dp[0] = 0; ` ` ` `dp[1] = 1; ` ` ` ` ` `// Every day current population ` ` ` `// will be three times of the previous day ` ` ` `for` `(` `int` `i = 2; i < 6; i++) ` ` ` `{ ` ` ` `dp[i] = dp[i - 1] * 3; ` ` ` `} ` ` ` ` ` `// Manually calculated value ` ` ` `dp[6] = 726; ` ` ` ` ` `// From 8th day onwards ` ` ` `for` `(` `int` `i = 8; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Chick population decreases by 2/3 everyday. ` ` ` `// For 8th day on [i-6] i.e 2nd day population ` ` ` `// was 3 and so 2 new born die on the 6th day ` ` ` `// and so on for the upcoming days ` ` ` `dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3; ` ` ` `} ` ` ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(getChicks(n)); ` `} ` `} ` ` ` `// This code has been contributed by @Tushil.. ` |

*chevron_right*

*filter_none*

**Output:**

9

**Efficient approach:** If you look closely, you can observe a pattern that is number of chicks for Nth day in the zoo can be calculated directly using the formula **pow(3, N – 1)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to return the number ` `// of chicks on the nth day ` `ll getChicks(` `int` `n) ` `{ ` ` ` ` ` `ll chicks = (ll)` `pow` `(3, n - 1); ` ` ` ` ` `return` `chicks; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << getChicks(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number ` `// of chicks on the nth day ` `static` `int` `getChicks(` `int` `n) ` `{ ` ` ` ` ` `int` `chicks = (` `int` `)Math.pow(` `3` `, n - ` `1` `); ` ` ` ` ` `return` `chicks; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println (getChicks(n)); ` `} ` `} ` ` ` `// This code is contributed by Tushil. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function to return the number ` `# of chicks on the nth day ` `def` `getChicks( n): ` ` ` ` ` `chicks ` `=` `pow` `(` `3` `, n ` `-` `1` `) ` ` ` ` ` `return` `chicks ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `3` ` ` ` ` `print` `( getChicks(n)) ` ` ` `# This code is contributed by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number ` ` ` `// of chicks on the nth day ` ` ` `static` `int` `getChicks(` `int` `n) ` ` ` `{ ` ` ` ` ` `int` `chicks = (` `int` `)Math.Pow(3, n - 1); ` ` ` ` ` `return` `chicks; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(getChicks(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of days remaining for the next day with higher temperature
- Maximize Profit by trading stocks based on given rate per day
- Find Nth positive number whose digital root is X
- Find the Nth Pure number
- Fast Doubling method to find the Nth Fibonacci number
- Find the Nth Hogben Numbers
- Program for nth Catalan Number
- nth Rational number in Calkin-Wilf sequence
- Number of ways to reach Nth floor by taking at-most K leaps
- Nth Fibonacci number using Pell's equation
- Compare sum of first N-1 elements to Nth element of an array
- Count ways to reach the Nth stair using any step from the given array
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum
- Construct an AP series consisting of A and B having minimum possible Nth term
- Nth term of a recurrence relation generated by two given arrays
- Count ways to reach the nth stair using step 1, 2 or 3
- Find the Number Occurring Odd Number of Times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.