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 :



title-img


                            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

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

View Solution →

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

View Solution →

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

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

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 →