# 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, d;
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;
}
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, i, j, k;
long int tot;
scanf("%d", &n);
for(i=0; i < n; i++)
scanf("%d%d", &a[i], &a[i]);
for(i = 0; i < n; i++) {
j = i;
k = n;
tot = 0;
while (k--) {
tot += a[j];
//  printf("## %d %d\n", tot, a[j]);
if (tot < a[j])
break;
tot -= a[j];
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))
dist.append(int(i))
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)```
```

## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing