forked from anshuman8800/Interivew-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetrol.cpp
More file actions
36 lines (31 loc) · 712 Bytes
/
petrol.cpp
File metadata and controls
36 lines (31 loc) · 712 Bytes
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
#include<iostream>
#include<vector>
using namespace std;
int main(){
cout<<"Enter the total number of petrol pumps"<<endl;
int n;cin>>n;
int A[n];
cout<<"Enter the distances of each petrol pump from Bongora"<<endl;
for(int i=0;i<n;i++){
cin>>A[i];
}
cout<<"Enter the value of mileage: "<<endl;
int c;cin>>c;
int cfuel=0;//nitialy filled the petrol at Bongora
int res[n];
int idx=0;
for(int i=1;i<n;i++){
if(cfuel<(A[i]-A[i-1])){
res[idx]=A[i];
idx++;
cfuel=c;
if(cfuel<A[i]-A[i-1]){
cout<<"Cannnot reach destination"; return 0;
}}
cfuel=cfuel-(A[i]-A[i-1]);
}
cout<<"Result:"<<endl;
for(int i=0;i<idx;i++){
cout<<res[i]<<" ";
}
}