PROBLEM LINK:CodeChef: Practical coding for everyone
Author: Abhishek Yadav
Tester: Abhishek Yadav
Editorialist: Abhishek Yadav
DIFFICULTY:
EASY
PREREQUISITES:
Math,Arrays.
PROBLEM:
As the COVID-19 Pandemic is on it’s Peak in India. The total number of Covid cases are increasing day by day. The Following series shows the number of cases on a particular day.
1,2,3,5,8,13,21,34,55,89,144,….
On the 1st Day the total cases where 1, on second day total cases where 2 , on third day 3 and so on.
You have to write a program which will calculate the total number of cases on the Nth day. According to the above series.
QUICK EXPLANATION:
You have to just add the last two values of array to form the current index value.
EXPLANATION:
Let’s understand the pattern with example
1,2,3,5,8,13,21,34,55,89,144,….
if you observe the above pattern for some time then you will find that the every 3rd index value is the addition of last two index values.
i.e a[4]=8 is the addition of its last two indexes a[4-1]+a[4-2]=5+3.
lastly you have to print the a[n-1] as stated in the question a[n-1] will be the total number of cases on the nth day.
you have to do this N times as stated in the question.
As the constraints are very small you can do this question in any complexity.
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int temp;
long long int arr[94];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
for(int i=3;i<94;i++)
{
arr[i] = arr[i-1] + arr[i-2];
}
cout<<arr[n-1]<<endl;
}
return 0;
}
Tester's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int temp;
long long int arr[94];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
for(int i=3;i<94;i++)
{
arr[i] = arr[i-1] + arr[i-2];
}
cout<<arr[n-1]<<endl;
}
return 0;
}
Editorialist's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int temp;
long long int arr[94];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
for(int i=3;i<94;i++)
{
arr[i] = arr[i-1] + arr[i-2];
}
cout<<arr[n-1]<<endl;
}
return 0;
}