forked from Shaily20/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainerShip.cpp
More file actions
40 lines (39 loc) · 777 Bytes
/
containerShip.cpp
File metadata and controls
40 lines (39 loc) · 777 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
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
int canCompleteCircuit(vector<int> &A, vector <int> &B){
int n=A.size();
if(n==1)
return 0;
int sum1=0,sum2=0,i,diff,maxa=0,maxele=0;
for(i=0;i<n;i++){
sum1+=(A[i]-B[i]);
sum2+=(A[i]-B[i]);
if(sum2<0){
maxele=i+1;
sum2=0;
}
}
if(sum1<0){
return -1;
}
return maxele%n;
}
int main()
{
int n,i,x;
printf("Enter n\n");
vector<int> gas,cost;
scanf("%d",&n);
printf("Enter gas at each station\n");
for(i=0;i<n;i++){
scanf("%d",&x);
gas.push_back(x);
}
printf("Enter costs\n");
for(i=0;i<n;i++){
scanf("%d",&x);
cost.push_back(x);
}
printf("Minimum starting Index is %d\n",canCompleteCircuit(gas,cost));
return 0;
}