I am solving the "Spread the Word" problem on CodeChef:
Snackdown 2019 is coming! People have started to spread the word and tell other people about the contest. There are \$N\$ people numbered 1 through \$N\$. Initially, only person 1 knows about Snackdown. On each day, everyone who already knows about Snackdown tells other people about it. For each valid \$i\$, person \$i\$ can tell up to \$A_i\$ people per day. People spread the information among the people who don't know about Snackdown in the ascending order of their indices; you may assume that no two people try to tell someone about Snackdown at the same moment. Each person is only allowed to start telling other people about Snackdown since the day after he/she gets to know about it (person 1 can start telling other people already on day 1). How many days does it take for all people to know about Snackdown?
But my code exceeds the given time limit. It solves the problem in 1.01 seconds, but the time limit in there, is 1.00 seconds. I am attaching the code snippet written in C++ below:
#include<iostream>
using namespace std;
int main()
{
int t,n;
cin>>t;
for(int i = 0; i<t;i++)
{
int c = 0,f=0,p=1;
cin>>n;
int a[n];
for(int i = 0; i<n; i++) cin>>a[i];
f = a[0];
while(p<n)
{
for(int i = 0; i<f; i++)
{
p += a[i];
}
c++;
f = p;
}
cout<<c;
}
}
It would be a great help if someone modifies the above code into an efficient one!