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 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++
// 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; } |
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. |
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 |
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. |
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. ?> |
Javascript
<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 km function 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 Code var N = 10; var K = 2; var M = 3; // compulsory petrol pumps to refill at var compulsory = [ 6, 7, 8 ]; // function call that returns the answer to the problem document.write( countRefill(N, K, M, compulsory)); </script> |
5
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Please Login to comment...