**Truck Tour**

### Problem Statement :

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each litre of the petrol. Input Format The first line will contain the value of N. The next N lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump. Output Format An integer which will be the smallest index of the petrol pump from which we can start the tour.

### Solution :

` ````
Solution in C :
In C ++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, p[100005], d[100005];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d%d", &p[i], &d[i]);
int ret = 0, amount = 0, sum = 0;
for (int i = 0; i < n; ++i) {
p[i] -= d[i];
sum += p[i];
if (amount + p[i] < 0) {
amount = 0;
ret = i + 1;
} else amount += p[i];
}
printf("%d\n", sum >= 0 ? ret : -1);
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<Integer> list = new ArrayList<Integer>();
int tank = 0;
int result = -1;
for(int loop=0; loop<n; loop++) {
int net = in.nextInt() - in.nextInt();
if(tank + net > 0) {
if(result==-1) {
result = loop;
}
list.add(net);
tank += net;
} else {
list.clear();
tank = 0;
result = -1;
}
}
System.out.println(result);
in.close();
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, a[100000][2], i, j, k;
long int tot;
scanf("%d", &n);
for(i=0; i < n; i++)
scanf("%d%d", &a[i][0], &a[i][1]);
for(i = 0; i < n; i++) {
j = i;
k = n;
tot = 0;
while (k--) {
tot += a[j][0];
// printf("## %d %d\n", tot, a[j][1]);
if (tot < a[j][1])
break;
tot -= a[j][1];
j ++;
if (j == n)
j = 0;
}
//printf("%d\n", k);
if (k == -1) {
printf("%d", j);
break;
}
}
return 0;
}
In Python3 :
num=int(input())
pet=[]
dist=[]
for line in range(num):
i=input().split(" ")
pet.append(int(i[0]))
dist.append(int(i[1]))
bal=[]
for i in range(num):
bal.append(pet[i]-dist[i])
small=0
for strt in range(num):
s=bal[strt]
i=(strt+1)%num
while(s>=0 and i!=strt):
s+=bal[i]
i=(i+1)%num
if(i==strt):
small=strt
break
print(small)
```

## View More Similar Problems

## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →## Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →## Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →## Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →