Open In App

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:
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++ 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 = 7;
 
    cout << getChicks(n);
 
    return 0;
}




// 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+1];
 
    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
public static void main(String[] args) {
int n = 7;
 
    System.out.println(getChicks(n));
    }
}
// This code has been contributed by 29AjayKumar




     
# 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+1);
  
    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[7] = 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 = 7;
  
print(getChicks(n));
 
# This code is contributed by Princi Singh




// 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+1];
 
    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
static public void Main ()
{
     
    int n = 7;
    Console.WriteLine(getChicks(n));
}
}
 
// This code has been contributed by @Tushil..




<script>
 
    // Javascript implementation of the approach
     
    // Function to return the number
    // of chicks on the nth day
    function getChicks(n)
    {
 
        // Size of dp[] has to be
        // at least 6 (1-based indexing)
        let size = Math.max(n, 7);
        let dp = new Array(size+1);
        dp.fill(0);
 
        dp[0] = 0;
        dp[1] = 1;
 
        // Every day current population
        // will be three times of the previous day
        for (let i = 2; i <= 6; i++)
        {
            dp[i] = dp[i - 1] * 3;
        }
 
        // Manually calculated value
        dp[7] = 726;
 
        // From 8th day onwards
        for (let 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 * parseInt(dp[i - 6] / 3, 10))) * 3;
        }
 
        return dp[n];
    }
     
    let n = 7;
    document.write(getChicks(n));
 
</script>

Output
726

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:

Below is the implementation of the above approach:




// 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
 
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;
}
 
// Driver code
int main()
{
    int n = 7;
 
    cout << NoOfChicks(n);
 
    return 0;
}




// Java implementation of the approach
import java.util.*;
 
public class GFG {
 
    // Function to return the number
    // of chicks on the nth day
    public static long NoOfChicks(int N)
    {
        // Code here
        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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 7;
 
        System.out.println(NoOfChicks(n));
    }
}




# Python implementation of the approach
 
# Function to return the number
# of chicks on the nth day
 
 
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]
 
 
# Driver code
n = 7
 
print(NoOfChicks(n))




// C# implementation of the approach
using System;
 
public class GFG {
 
  // Function to return the number
  // of chicks on the nth day
  public static long NoOfChicks(int N)
  {
    // Code here
    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()
  {
 
    // Code
    int n = 7;
    Console.WriteLine(NoOfChicks(n));
  }
}
 
// This code is contributed by lokesh




    // Function to return the number
    // of chicks on the nth day
    function NoOfChicks(N)
    {
        // Code here
        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));
 
// This code is contributed by utkarshshirode02

Output
726

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:

Below is the implementation of the above approach:




/// C++ implementation
#include<bits/stdc++.h>
using namespace std;
 
 
class GFG
{
    int findIndex (int k)
    {
          if(k%6 == 0)
        {
              ///returning the end location which is 6;
              return 6;
        }
          else
        {
            /// returning the mod of 6 to maintain the array location from 1 to 5.
              return (k%6);
        }
    }
     
public:
    long long int NoOfChicks(int N)
    {
        /// if n == 1
        if(N == 1)
        {
            return 1;
        }
         
          /// declearing newBornChicks array and totalChicks
        long long int  newBorn[7], totalChicks;
         
          /// initialising by 1
        newBorn[1] = totalChicks = 1;
         
         
          /// loop will run from 2 to n (inclusive).
        for(int i = 2; i <= N; i++)
        {
            if(i <= 6)
            {
                  /// instead of multiply by two, using leftShift by 1.
                  /// storing today's newBornChicks at index i.
                newBorn[i] = totalChicks<<1;
                  /// updating today total alive chicks
                totalChicks += newBorn[i];
            }
            else
            {
                int index = findIndex(i);
                 
                /// subtracting dead chicks from total chicks present today.
                totalChicks -= newBorn[index];
                 
                /// instead of multiply by two, using leftShift by 1.
                newBorn[index] = totalChicks<<1;
                  /// updating today total alive chicks
                totalChicks += newBorn[index];
            }
        }
        return totalChicks;
    }
};
 
//{ Driver Code Starts.
int main()
{
    int N = 7;
      GFG obj;
    cout << obj.NoOfChicks(N) <<"\n";
    return 0;
}
// } Driver Code Ends




/// Java implementation
public class GFG
{
    private static int findIndex (int k)
    {
        if(k%6 == 0)
        {
              ///returning the end location which is 6;
              return 6;
        }
          else
        {
            /// returning the mod of 6 to maintain the array location from 1 to 5.
              return (k%6);
        }
    }
     
    public static long NoOfChicks(int N)
    {
        if(N == 1)
        {
            return 1;
        }
         
        /// declearing newBornChicks array and totalChicks
        long []newBorn = new long[7];
        long totalChicks;
         
        /// initialising by 1
        newBorn[1] = totalChicks = 1;
         
         
        for(int i = 2; i <= N; i++)
        {
            if(i <= 6)
            {
                /// instead of multiply by two, using leftShift by 1.
                  /// storing today's newBornChicks at index i.
                newBorn[i] = totalChicks<<1;
                  /// updating today total alive chicks
                totalChicks += newBorn[i];
            }
            else
            {
                int index = findIndex(i);
                 
                  /// subtracting dead chicks from total chicks present today.
                totalChicks -= newBorn[index];
                 
                /// instead of multiply by two, using leftShift by 1.
                newBorn[index] = totalChicks<<1;
                  /// updating today total alive chicks
                totalChicks += newBorn[index];
            }
        }
         
        return totalChicks;
    }
   
   
      /// main class
      public static void main(String[] args)
    {
        int n = 7;
  
        System.out.println(NoOfChicks(n));
    }
}




# Python implementation
def findIndex(k):
 
    if(k % 6 == 0):
        # returning the end location which is 6;
        return 6
    else:
 
        # returning the mod of 6 to maintain the array location from 1 to 5.
        return (k % 6)
 
def NoOfChicks(N):
 
    # if n == 1
    if(N == 1):
        return 1
 
    # ndeclearing newBornChicks array and totalChicks
    newBorn = []
    totalChicks = 0
    for i in range(0, 7):
        newBorn.append(0)
 
    # initialising by 1
    newBorn[1] = totalChicks = 1
 
    # loop will run from 2 to n (inclusive)
    for i in range(2, N + 1):
        if(i <= 6):
            # instead of multiply by two, using leftShift by 1.
            # storing today's newBornChicks at index i.
            newBorn[i] = totalChicks << 1
 
            # updating today total alive chicks
            totalChicks += newBorn[i]
 
        else:
            index = findIndex(i)
 
            # subtracting dead chicks from total chicks present today.
            totalChicks -= newBorn[index]
 
            # instead of multiply by two, using leftShift by 1.
            newBorn[index] = totalChicks << 1
 
            # updating today total alive chicks
            totalChicks += newBorn[index]
 
    return totalChicks
 
# Driver Code
N = 7
print(NoOfChicks(N))
 
# This code is contributed by Samim Hossain Mondal.




// Include namespace system
using System;
 
// / C# implementation
public class GFG
{
    private static int findIndex(int k)
    {
        if (k % 6 == 0)
        {
            // /returning the end location which is 6;
            return 6;
        }
        else
        {
            // / returning the mod of 6 to maintain the array location from 1 to 5.
            return (k % 6);
        }
    }
    public static long NoOfChicks(int N)
    {
        if (N == 1)
        {
            return 1;
        }
        // / declearing newBornChicks array and totalChicks
        long[] newBorn = new long[7];
        long totalChicks;
        // / initialising by 1
        newBorn[1] = totalChicks = 1;
        for (int i = 2; i <= N; i++)
        {
            if (i <= 6)
            {
                // / instead of multiply by two, using leftShift by 1.
                // / storing today's newBornChicks at index i.
                newBorn[i] = totalChicks << 1;
                // / updating today total alive chicks
                totalChicks += newBorn[i];
            }
            else
            {
                var index = GFG.findIndex(i);
               
                // / subtracting dead chicks from total chicks present today.
                totalChicks -= newBorn[index];
               
                // / instead of multiply by two, using leftShift by 1.
                newBorn[index] = totalChicks << 1;
               
                // / updating today total alive chicks
                totalChicks += newBorn[index];
            }
        }
        return totalChicks;
    }
   
    // / main class
    public static void Main(String[] args)
    {
        var n = 7;
        Console.WriteLine(GFG.NoOfChicks(n));
    }
}
 
// This code is contributed by utkarshshirode02




// Javascript implementation
    function findIndex (k)
    {
          if(k%6 == 0)
        {
         
              ///returning the end location which is 6;
              return 6;
        }
          else
        {
         
            /// returning the mod of 6 to maintain the array location from 1 to 5.
              return (k%6);
        }
    }
     
function  NoOfChicks( N)
    {
        /// if n == 1
        if(N == 1)
        {
            return 1;
        }
         
          /// declearing newBornChicks array and totalChicks
        let  newBorn =[], totalChicks;
        for(let i = 0; i < 7; i++)
            newBorn.push(0);
         
          /// initialising by 1
        newBorn[1] = totalChicks = 1;
         
         
          /// loop will run from 2 to n (inclusive).
        for(let i = 2; i <= N; i++)
        {
            if(i <= 6)
            {
                  /// instead of multiply by two, using leftShift by 1.
                  /// storing today's newBornChicks at index i.
                newBorn[i] = totalChicks<<1;
                 
                  /// updating today total alive chicks
                totalChicks += newBorn[i];
            }
            else
            {
                let index = findIndex(i);
                 
                /// subtracting dead chicks from total chicks present today.
                totalChicks -= newBorn[index];
                 
                /// instead of multiply by two, using leftShift by 1.
                newBorn[index] = totalChicks<<1;
                 
                  /// updating today total alive chicks
                totalChicks += newBorn[index];
            }
        }
        return totalChicks;
    }
 
 
    let N = 7;
      console.log(NoOfChicks(N));
       
      // This code is contributed by garg28harsh.

Output
726

Time Complexity: O(N).
Auxiliary Space: O(1).


Article Tags :