Number of refills to complete the journey of N km

04 May, 2021
Given a number N which represents the total distance in km to be covered by a car on a single road. There are N petrol pumps at a distance of 1 km each(1, 2, 3, ..N). The capacity of the fuel tank of the car is such that at full tank it goes till a distance of K km. The car has to compulsorily stop at M petrol tanks whose distance from the starting position is given as M integers. The task is to find the number of times, the car has to refill its tank including the compulsory stops to complete its journey of N km. Examples :  
Input: N = 5, K = 2, M = 1 arr[] = {3} Output: 2 The car starts at 0, with its tank full, travels for 2 km and then refills its tank at 2 km. The car makes the compulsory stop at 3 where its tank if refilled again. It travels for 2 km more to reach its destination of 5 km. Input: N = 10, K = 2, M = 3 arr[] = { 6, 7, 8 } Output: 5 The car starts from 0, stops at 2, 4 for refilling its tank, before making compulsory stops at 6, 7, 8. It travels 2 km from 8 km to make its journey of 10 km complete. 
 
Approach: As total journey is of N km, so keep a track of distance covered till now in a variable, say distCovered, which will be initialized by 0. Increment distCovered by K km till distCovered is less than N because K is the amount of distance vehicle can travel since the last refill. Also, with each increment, check if there is a compulsory petrol pump to stop between distCovered and distCovered + K, if yes, then distCovered will be K plus the last compulsory petrol pump to stop at between distCovered and distCovered + K. Also, keep counting the number of refills taken to reach the destination of N km. Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program for finding the total// number of stops for refilling to// reach destination of N km#include <iostream>using namespace std; // Function that returns the total number of// refills made to reach the destination of N kmint countRefill(int N, int K, int M, int compulsory[]){    int count = 0;    int i = 0;    int distCovered = 0;     // While we complete the whole journey.    while (distCovered < N) {        // If must visited petrol pump lie        // between distCovered and distCovered+K.        if (i < M && compulsory[i] <= (distCovered + K)) {            // make last mustVisited as distCovered            distCovered = compulsory[i];             // increment the index of compulsory visited.            i++;        }         // if no such must visited pump is        // there then increment distCovered by K.        else            distCovered += K;         // Counting the number of refill.        if (distCovered < N)            count++;    }     return count;} // Driver Codeint main(){    int N = 10;    int K = 2;    int M = 3;    // compulsory petrol pumps to refill at    int compulsory[] = { 6, 7, 8 };     // function call that returns the answer to the problem    cout << countRefill(N, K, M, compulsory) << endl;    return 0;}
// Java program for finding the// total number of stops for// refilling to reach// destination of N kmimport java.io.*; class GFG{     ; // Function that returns the// total number of refills made// to reach the destination of N kmstatic int countRefill(int N, int K,                       int M, int compulsory[]){    int count = 0;    int i = 0;    int distCovered = 0;     // While we complete    // the whole journey.    while (distCovered < N)    {        // If must visited petrol pump lie        // between distCovered and distCovered+K.        if (i < M && compulsory[i] <=                                (distCovered + K))        {            // make last mustVisited            // as distCovered            distCovered = compulsory[i];             // increment the index            // of compulsory visited.            i++;        }         // if no such must visited        // pump is there then        // increment distCovered by K.        else            distCovered += K;         // Counting the number of refill.        if (distCovered < N)            count++;    }     return count;} // Driver Codepublic static void main (String[] args){    int N = 10;    int K = 2;    int M = 3;    // compulsory petrol    // pumps to refill at    int compulsory[] = { 6, 7, 8 };         // function call that returns    // the answer to the problem    System.out.println(countRefill(N, K,                        M, compulsory));}} // This code is contributed by anuj_67.
# Python 3 program for finding the total# number of stops for refilling to reach# destination of N km # Function that returns the total number of# refills made to reach the destination of N kmdef countRefill(N, K, M, compulsory):    count = 0    i = 0    distCovered = 0     # While we complete the whole journey.    while (distCovered < N):                 # If must visited petrol pump lie        # between distCovered and distCovered+K.        if (i < M and compulsory[i] <= (distCovered + K)):                         # make last mustVisited as distCovered            distCovered = compulsory[i]             # increment the index of            # compulsory visited.            i += 1         # if no such must visited pump is        # there then increment distCovered by K.        else:            distCovered += K         # Counting the number of refill.        if (distCovered < N):            count += 1     return count # Driver Codeif __name__ == '__main__':    N = 10    K = 2    M = 3         # compulsory petrol pumps to refill at    compulsory = [6, 7, 8]     # function call that returns the    # answer to the problem    print(countRefill(N, K, M, compulsory))     # This code is contributed by# Sanjit_Prasad
// C# program for finding the// total number of stops for// refilling to reach// destination of N kmusing System; class GFG{     // Function that returns// the total number of// refills made to reach// the destination of N kmstatic int countRefill(int N, int K,                       int M, int []compulsory){    int count = 0;    int i = 0;    int distCovered = 0;     // While we complete    // the whole journey.    while (distCovered < N)    {        // If must visited petrol pump        // lie between distCovered and        // distCovered+K.        if (i < M && compulsory[i] <=                (distCovered + K))        {            // make last mustVisited            // as distCovered            distCovered = compulsory[i];             // increment the index            // of compulsory visited.            i++;        }         // if no such must visited        // pump is there then        // increment distCovered by K.        else            distCovered += K;         // Counting the number of refill.        if (distCovered < N)            count++;    }     return count;} // Driver Codepublic static void Main (){    int N = 10;    int K = 2;    int M = 3;         // compulsory petrol    // pumps to refill at    int []compulsory = {6, 7, 8};         // function call that returns    // the answer to the problem    Console.WriteLine(countRefill(N, K,                       M, compulsory));}} // This code is contributed by anuj_67.
<?php// PHP program for finding the total// number of stops for refilling to// reach destination of N km // Function that returns the total// number of refills made to reach// the destination of N kmfunction countRefill($N, $K, $M,                     $compulsory){    $count = 0;    $i = 0;    $distCovered = 0;     // While we complete    // the whole journey.    while ($distCovered < $N)    {        // If must visited petrol        // pump lie between distCovered        // and distCovered + K.        if ($i < $M and $compulsory[$i] <=                       ($distCovered + $K))        {            // make last mustVisited            // as distCovered            $distCovered = $compulsory[$i];             // increment the index            // of compulsory visited.            $i++;        }         // if no such must visited        // pump is there then        // increment distCovered by K.        else            $distCovered += $K;         // Counting the number        // of refill.        if ($distCovered < $N)            $count++;    }     return $count;} // Driver Code$N = 10;$K = 2;$M = 3; // compulsory petrol// pumps to refill at$compulsory = array(6, 7, 8); // function call that returns// the answer to the problemecho countRefill($N, $K, $M,                 $compulsory) , "\n"; // This code is contributed by anuj_67.?>
<script> // Javascript program for finding the total// number of stops for refilling to// reach destination of N km // Function that returns the total number of// refills made to reach the destination of N kmfunction countRefill(N, K, M, compulsory){    var count = 0;    var i = 0;    var distCovered = 0;     // While we complete the whole journey.    while (distCovered < N) {        // If must visited petrol pump lie        // between distCovered and distCovered+K.        if (i < M && compulsory[i] <= (distCovered + K))        {            // make last mustVisited as distCovered            distCovered = compulsory[i];             // increment the index of compulsory visited.            i++;        }         // if no such must visited pump is        // there then increment distCovered by K.        else            distCovered += K;         // Counting the number of refill.        if (distCovered < N)            count++;    }     return count;} // Driver Codevar N = 10;var K = 2;var M = 3; // compulsory petrol pumps to refill atvar compulsory = [ 6, 7, 8 ]; // function call that returns the answer to the problemdocument.write( countRefill(N, K, M, compulsory)); </script>
5
 
Time Complexity: O(N) Auxiliary Space: O(1) 
vt_m
Sanjit_Prasad
rrrtnx
Constructive Algorithms
cpp-array
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.