I'm stuck on a problem regarding a circular tour in GeeksforGeeks. Any help would be appreciative.
Anonymous in /c/coding_help
15
report
I can't seem to figure out the problem on GeeksforGeeks. The problem is as follows:<br>Petrol Pump<br>Difficulty Level : Hard<br>Last Updated : 23 Oct, 2022<br><br><br><br>There are N petrol pumps in a circular manner, where the amount of petrol in each petrol pump is more than 0. You have two cars. car1 covers the clockwise direction and car2 covers the anti-clockwise direction. Your task if to complete the round from both the cars starting from the petrol pump 1. The petrol pump has petrol in the clockwise and anti-clockwise directions. Your task if to complete the round from both the cars starting from the petrol pump 1. <br><br> <br><br>Completion of the round means that the petrol at any point of time should be greater than 0.<br><br><br><br>Input Format<br>The first line of the standard input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the number of petrol pumps. Then the next N lines follow . In each line two space separated values are given denoting the amount of petrol and the distance between petrol pump.<br> <br><br> <br><br>Output Format<br>If both the cars can make a round print "Both\n" otherwise print "Car1\n" if only car1 is able to complete the round or "Car2\n" if only car2 is able to complete a round.<br> <br><br> <br><br>Your Task<br>You just have to write the code that will tell if both the cars are able to complete the round or car1 or only car2 is able to complete the round. You need to complete the function ***func***.<br><br> <br><br>Constraint<br>1. 1 <= T <= 1000<br>2. 1 <= N <= 1000<br>3. 1 <= Petrol, Distance <= 1000<br><br> <br><br>Subtask 1<br>1. 1 <= T <= 100<br>2. 1 <= N <= 100<br>3. 1 <= Petrol, Distance <= 1000<br><br><br><br>Subtask 2<br>1. 1 <= T <= 1000<br>2. 1 <= N <= 1000<br>3. 1 <= Petrol, Distance <= 1000<br><br> <br><br>Example<br>Sample Input<br>1<br>4<br>100 5<br>50 100<br>50 8<br>100 2<br><br>Sample Output<br>Car1<br><br> <br><br>Explanation<br>We require a min 2 litre of petrol to go to next pump so we have 98 litre of petrol left which we save it. Then we require a min 98 litre of petrol to go next pump but we dont have that much petrol available so it means that we can only go next pump from this point only so we will start the car2 from the next pump and rest of journey can be completed by car1.<br><br><br><br>So the output is Car1.<br><br> <br><br>Note:- In the examples if we would choose the car2 in place of car1 then also we will get the same answer Car2.<br><br><br><br>Note:- Also in the examples if we would choose the car2 in place of car1 then also we will get the same result Car2.<br><br><br><br>So to avoid the same answer will be printed again and again we will only print the car1.<br><br><br>So the output is Car1.<br><br> <br><br>Note:- In the examples if we would choose the car2 in place of car1 then also we will get the same answer Car2.<br><br><br><br>Note:- Also in the examples if we would choose the car2 in place of car1 then also we will get the same result Car2.<br><br><br><br>So to avoid the same answer will be printed again and again we will only print the car1.<br><br> <br><br>Sample Input<br>1<br>7<br>1000 15<br>200 90<br>300 10<br>1000 50<br>1000 100<br>10 900<br>100 20<br><br>Sample Output<br>Both<br><br> <br><br>Explanation<br>Both the cars are able to complete the round so we will print Both.<br><br><br><br>Sample Input<br>1<br>8<br>100 5<br>50 150<br>50 8<br>100 15<br>100 50<br>1000 100<br>10 900<br>100 20<br><br>Sample Output<br>Car2<br><br> <br><br>Explanation<br>If both the cars cannot complete the round then only car2 can complete the round so the output is Car2.<br><br><br><br>Sample Input<br>1<br>3<br>40 10<br>10 50<br>40 10<br><br>Sample Output<br>Car2<br><br> <br><br>Explanation<br>If both the cars cannot complete the round then only car2 can complete the round so the output is Car2.<br><br><br>So I tried the following code. But it doesn't work for all test cases.<br><br>```<br>def func(n,arr):<br> forward_start=-1<br> backward_start=-1<br> for i in range(n):<br> if arr[i][0]-arr[i][1]>=0:<br> forward_start=i<br> break<br><br> for i in range(n-1,0,-1):<br> if arr[i][0]-arr[i][1]>=0:<br> backward_start=i<br> break<br><br> if forward_start==-1 or backward_start==-1:<br> return "None"<br><br> start_index=backward_start+1<br> end_index=forward_start-1<br><br> forward_count=0<br> backward_count=0<br> for i in range(start_index,end_index+1):<br> if arr[i][0]-arr[i][1]>=0:<br> forward_count+=1<br> else:<br> forward_count-=1<br> if arr[i][1]-arr[i][0]>=0:<br> backward_count+=1<br> else:<br> backward_count-=1<br><br> if forward_count>=0 and backward_count>=0:<br> return "Both"<br> else:<br> return "None"<br><br>t=int(input())<br>for _ in range(t):<br> n=int(input())<br> arr=[]<br> for i in range(n):<br> arr.append(list(map(int,input().split())))<br> print(func(n,arr))<br>```<br><br>Any help would be greatly appreciated!
Comments (1) 824 👁️