Number of refills to complete the journey of N km
Last Updated :
22 Aug, 2022
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++
#include <iostream>
using namespace std;
int countRefill( int N, int K, int M, int compulsory[])
{
int count = 0;
int i = 0;
int distCovered = 0;
while (distCovered < N) {
if (i < M && compulsory[i] <= (distCovered + K)) {
distCovered = compulsory[i];
i++;
}
else
distCovered += K;
if (distCovered < N)
count++;
}
return count;
}
int main()
{
int N = 10;
int K = 2;
int M = 3;
int compulsory[] = { 6, 7, 8 };
cout << countRefill(N, K, M, compulsory) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
;
static int countRefill( int N, int K,
int M, int compulsory[])
{
int count = 0 ;
int i = 0 ;
int distCovered = 0 ;
while (distCovered < N)
{
if (i < M && compulsory[i] <=
(distCovered + K))
{
distCovered = compulsory[i];
i++;
}
else
distCovered += K;
if (distCovered < N)
count++;
}
return count;
}
public static void main (String[] args)
{
int N = 10 ;
int K = 2 ;
int M = 3 ;
int compulsory[] = { 6 , 7 , 8 };
System.out.println(countRefill(N, K,
M, compulsory));
}
}
|
Python3
def countRefill(N, K, M, compulsory):
count = 0
i = 0
distCovered = 0
while (distCovered < N):
if (i < M and compulsory[i] < = (distCovered + K)):
distCovered = compulsory[i]
i + = 1
else :
distCovered + = K
if (distCovered < N):
count + = 1
return count
if __name__ = = '__main__' :
N = 10
K = 2
M = 3
compulsory = [ 6 , 7 , 8 ]
print (countRefill(N, K, M, compulsory))
|
C#
using System;
class GFG
{
static int countRefill( int N, int K,
int M, int []compulsory)
{
int count = 0;
int i = 0;
int distCovered = 0;
while (distCovered < N)
{
if (i < M && compulsory[i] <=
(distCovered + K))
{
distCovered = compulsory[i];
i++;
}
else
distCovered += K;
if (distCovered < N)
count++;
}
return count;
}
public static void Main ()
{
int N = 10;
int K = 2;
int M = 3;
int []compulsory = {6, 7, 8};
Console.WriteLine(countRefill(N, K,
M, compulsory));
}
}
|
PHP
<?php
function countRefill( $N , $K , $M ,
$compulsory )
{
$count = 0;
$i = 0;
$distCovered = 0;
while ( $distCovered < $N )
{
if ( $i < $M and $compulsory [ $i ] <=
( $distCovered + $K ))
{
$distCovered = $compulsory [ $i ];
$i ++;
}
else
$distCovered += $K ;
if ( $distCovered < $N )
$count ++;
}
return $count ;
}
$N = 10;
$K = 2;
$M = 3;
$compulsory = array (6, 7, 8);
echo countRefill( $N , $K , $M ,
$compulsory ) , "\n" ;
?>
|
Javascript
<script>
function countRefill(N, K, M, compulsory)
{
var count = 0;
var i = 0;
var distCovered = 0;
while (distCovered < N) {
if (i < M && compulsory[i] <= (distCovered + K))
{
distCovered = compulsory[i];
i++;
}
else
distCovered += K;
if (distCovered < N)
count++;
}
return count;
}
var N = 10;
var K = 2;
var M = 3;
var compulsory = [ 6, 7, 8 ];
document.write( countRefill(N, K, M, compulsory));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...