-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 33: Chicks in a Zoo
More file actions
83 lines (66 loc) · 1.91 KB
/
Day 33: Chicks in a Zoo
File metadata and controls
83 lines (66 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Chicks in a Zoo
Initially, the zoo have a single chick. A chick gives birth to 2 chicks every day and the life expectancy of a chick is 6 days. Zoo officials want to buy food for chicks so they want to know the number of chicks on an Nth day. Help the officials with this task.
Example 1:
Input: N = 2
Output: 3
Explanation: First day there is only 1 chick.
On second day total number of chicks = 3.
Example 2:
Input: N = 3
Output: 9
Explanation: First day there is only 1 chick.
On second day total number of chicks = 3.
On third day total number of chicks = 9
Your Task:
You don't need to read or print anything, Your task is to complete the function NoOfChicks() which takes N as the input parameter and returns the total no. of chicks on the Nth day.
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
Constraints:
1 <= N <= 35
*/
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-->0)
{
int N = Integer.parseInt(br.readLine().trim());
Solution obj = new Solution();
long ans = obj.NoOfChicks(N);
System.out.println(ans);
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
public 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;
}
}