# Number of refills to complete the journey of N km

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

**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.**

*distCovered*Below is the implementation of the above approach:

## C++

`// 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 km ` `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 Code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for finding the ` `// total number of stops for ` `// refilling to reach ` `// destination of N km ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `; ` ` ` `// Function that returns the ` `// total number of refills made ` `// to reach the destination of N km ` `static` `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 Code ` `public` `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. ` |

*chevron_right*

*filter_none*

## Python3

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

def 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 Code
if __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
[tabby title="C#"]

`// C# program for finding the ` `// total number of stops for ` `// refilling to reach ` `// destination of N km ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns ` `// the total number of ` `// refills made to reach ` `// the destination of N km ` `static` `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 Code ` `public` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?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 km ` `function` `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 problem ` `echo` `countRefill(` `$N` `, ` `$K` `, ` `$M` `, ` ` ` `$compulsory` `) , ` `"\n"` `; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5

**Time Complexity**: O(N)

**Auxiliary Space**: O(1)

## Recommended Posts:

- Minimum number of days required to complete the work
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Count number of triplets with product equal to given number with duplicates allowed
- Maximum number of contiguous array elements with same number of set bits
- Count number of triplets with product equal to given number
- Count number of triplets with product equal to given number | Set 2
- Number of steps required to convert a binary number to one
- Count Number of animals in a zoo from given number of head and legs
- Find the Number Occurring Odd Number of Times
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Number whose XOR sum with given array is a given number k
- Number of NGEs to the right
- Number of subarrays having sum less than K
- Number of subarrays with odd sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.