Find the number of Chicks in a Zoo at Nth day | Function to return the number of chicks on the nth day ; Size of dp [ ] has to be at least 6 ( 1 - based indexing ) ; Every day current population will be three times of the previous day ; Manually calculated value ; From 8 th day onwards ; Chick population decreases by 2 / 3 everyday . For 8 th day on [ i - 6 ] i . e 2 nd day population was 3 and so 2 new born die on the 6 th day and so on for the upcoming days ; Driver code
function getChicks ( n ) { let size = Math . max ( n , 7 ) ; let dp = new Array ( size ) ; dp . fill ( 0 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i < 6 ; i ++ ) { dp [ i ] = dp [ i - 1 ] * 3 ; } dp [ 6 ] = 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 = 3 ; document . write ( getChicks ( n ) ) ;