Help me in solving PTRLP problem

My issue

I havent understood what the problem is asking me to do…on what basis is the starting point being chosen? we dont have the maximum amount of money which can be spent

My code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t; cin>>t ;
	while(t--){
	    long long n ;
	    cin>>n ;
	    long long petrol[n], cost[n] ;
	    for(int i = 0 ; i<n ; i++){
	        cin>>petrol[i] ;
	    }
	    
	    for(int i = 0 ; i<n ; i++){
	        cin>>cost[i] ;
	    }
	}
	return 0;
}

Problem Link: CodeChef: Practical coding for everyone

@shylamadan
The question states that to go from ith to (i+1)th station it require cost[i] petrol and petrol available at ith station is p[i] .
So u can only complete the trip when around the trip the sum of p[i]>=cost[i] else u can’t complete the trip .
I have coded the same intuition hope u will get it.

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int n;
	cin>>n;
	int a[n],b[n];
	int ans=-1;
	for(int i=0;i<n;i++)
	{
	    cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
	    cin>>b[i];
	}
	for(int i=0;i<n;i++)
	{
	    if(a[i]>=b[i])
	    {
	        int sma=a[i];
	        int smb=b[i];
	        int j=(i+1)%n;
	        int ch=0;
	        while(j!=i)
	        {
	            sma+=a[j];
	            smb+=b[j];
	            if(sma<smb)
	            {
	                ch=1;
	                break;
	            }
	            j=(j+1)%n;
	        }
	        if(ch==1)
	        {
	            if(j<i)
	            {
	                break;
	            }
	            i=j;
	        }
	        else
	        {
	            ans=i;
	            break;
	        }
	    }
	}
	cout<<ans;
	return 0;
}
1 Like