Feedback for PTRLP problem

Problem Link: CodeChef: Practical coding for everyone

Feedback

question is not clear what to achieve exactly.

@ramandwivedi
The question states that , u want to go around the world trip once and u can begin from any of the city .
U have initially empty tank the petrol available at the ith city is pi and the petrol required to go from ith to (i+1)th city is ci.
Your task is to find the city to begin with so that u can complete your world trip once.

#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];
	            //cout<<j<<endl;
	          //  cout<<sma<<" "<<smb<<endl;
	            if(sma<smb)
	            {
	                ch=1;
	                break;
	            }
	            j=(j+1)%n;
	           // cout<<j<<endl;
	        }
	        if(ch==1)
	        {
	            if(j<i)
	            {
	                break;
	            }
	            i=j;
	        }
	        else
	        {
	            ans=i;
	            break;
	        }
	    }
	}
	cout<<ans;
	return 0;
}

This is my code . Hope u will get the logic . I have done it in much simpler way.

1 Like