# 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;
}
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)```
```

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

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

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

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's