Sorry there is some correction....
p[] : array of petrol
d[]: array of distance
int FirstPetrolPump(int p[],int d[],int n)
{
int c=0,ThisPetrol=0,FirstPump=0,i;
for(i=0;i<n && c<n; i++)
{
ThisPetrol+=p[i];
if(ThisPetrol<d[i])
{
ThisPetrol=0;
FirstPump=i+1;
c=0;
}
else
{
*ThisPetrol-=d[i];*
c++;
}
}
while(c<n)
{
ThisPetrol+=p[i%n];
if(ThisPetrol<d[i%n])
return -1;
* **ThisPetrol-=d[i];*
i++;
c++;
}
return FirstPump;
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.