**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

## Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →