-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpetrol.c
51 lines (47 loc) · 959 Bytes
/
petrol.c
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
#include<stdio.h>
int main(){
int i,n,liter[10],flag=0,distance[10],cnt,travel,j,k;
n=4;
for(i=0;i<n;i++){
scanf("%d",&liter[i]);
scanf("%d",&distance[i]);
}
for(i=0;i<n;i++){
cnt=0;
travel=0;
for(j=i,k=0;k<=n;k++,j=j%n){
//j=i from which petrol pump
//j%n e.g j=5 5%4=1 to round off to below n
if(k==n){
if(cnt==n){printf("\nfeasible from %d",i);
flag=1;}
break;
}
if(liter[j] > distance[j] || travel!=0){
travel = travel+liter[j]-distance[j];
cnt++;
}else{
//printf("\nnot feasible from %d",i);
break;
}
j++;
}
if(flag)
break;
}
//another soluton for above problem is below but it does not satisfy all test case if you have efficient one comment it below
/*i=0;
while(1){
if(cnt==n)
break;
i=i%n;
if(liter[i] > distance[i] || travel != 0){
printf("pongal : %d\t",i);
travel=travel+liter[i]-distance[i];
cnt++;
}//else{
//}
i++;
}*/
return 0;
}