Header Ad

HackerRank Truck Tour problem solution

In this HackerRank Truck Tour problem, we have given the number of petrol pumps and the distance between each petrol pump, and the amount of petrol at every petrol. we just need to find the smallest index of the petrol pump from which we can start the tour.

HackerRank Truck Tour problem solution


Problem solution in Python programming.

a=[]

for i in range(int(input())):
    a.append(list(map(int, input().split())))
    a[i] = a[i][0] - a[i][1]

sum = 0
fin = 0
shift = 0
while (fin < len(a)):
    sum+=a[fin]
    if sum < 0:
        for _ in range(fin + 1):
            a.append(a.pop(0))
        sum = 0
        shift += fin + 1
        fin=0
    else:
        fin = fin + 1
            
print (shift)
        


Problem solution in Java Programming.

import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {


		 Scanner sc = new Scanner(System.in);
		
		 int N = sc.nextInt();
		
		 long[] a = new long[N];
		
		 for (int i = 0; i < N; i++) {
		 a[i] = sc.nextLong() - sc.nextLong();
		
		 }

		int result = solve(a);
		System.out.println(result);

	}

	public static int solve(long[] pumps) {

		int i = 0;
		while (true) {

			while (pumps[i] < 0) {
				i = (i + 1) % pumps.length;
			}
			long temp = 0;
			int j = i - 1;
			while (temp >= 0) {
				if (i == j) {
					return i + 1;
				}
				temp += pumps[i];
				i = (i + 1) % pumps.length;
			}

		}

	}

}


Problem solution in C++ programming.

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


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


static int *gas;
static int *dist;

int find_starting_point(int *gas, int *dist, int count) {
    int i;
    int start = 0;
    int end = 1;
    
    int gas_left = gas[start] - dist[start];
    while((start != end) || (gas_left < 0)) {
        while ((start != end) && (gas_left < 0)) {
            gas_left -= gas[start] - dist[start];
            start = (start+1)%count;
            if (start == 0)
                return -1;
        } 

        gas_left += gas[end] - dist[end];
        end = (end+1)%count;
     }
    
    return(start);
}

int main() {
   int _ar_size;
   int _ar_i;
    
   scanf("%d\n", &_ar_size);
   gas = (int *)malloc(sizeof(int)*_ar_size);
   if (gas == NULL)
       return 0;
    
   dist = (int *)malloc(sizeof(int)*_ar_size);
   if (dist == NULL) {
       free(gas);
       return 0;
   }
    
   for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { 
      scanf("%d %d", &gas[_ar_i], &dist[_ar_i]); 
   }
    
   #if 0
   for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { 
      printf("%d %d\n", gas[_ar_i], dist[_ar_i]); 
   }
   #endif
    
   printf("%d", find_starting_point(gas, dist, _ar_i));
    
   free(gas);
   free(dist);
    
   return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    input = prepareInput(input);
    
    var totalDiff = 0;
    var minDiff = 0;
    var minIndex = 0;
    
    for (var i = 1; i <= input[0][0]; i++) {
        totalDiff += input[i][0] - input[i][1];
        if (totalDiff < minDiff) {
            minDiff = totalDiff;
            minIndex = i;
        }
    }
    
    console.log(minIndex);
} 

function prepareInput(input) {
    input = input.split("\n").map(function(e){
        return e.split(" ").map(Number);
    });
    return input;
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments