fork(4) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int getTour(int petrol[],int distance[], int n)
  5. {
  6. int sum=0,start=0,diff=0;
  7. for(int i=0;i<n;i++)
  8. {
  9. sum=sum+petrol[i]-distance[i];
  10.  
  11. if(sum<0)
  12. {
  13. start=i+1;//changing start point
  14. diff+=sum;//storing the negative values
  15. sum=0;//starting again form new station
  16. }
  17. }
  18. return sum+diff>0? start: -1;//if sum+diff <0 which means the petrol and distance is not matched
  19. }
  20.  
  21. int main()
  22. {
  23. int petrol[4]={4,6,7,4};
  24. int distance[4]={6,5,3,5};
  25.  
  26. int value=getTour(petrol,distance,4);
  27. printf("%d\n",value);
  28. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1