Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Truck Tour problem solution

YASH PAL, 31 July 20241 September 2024

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 solution
HackerRank truck tour 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("%dn", 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("%dn", &_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 %dn", 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);
});

 

coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes