Number of circular tours that visit all petrol pumps | Javascript Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Drivers code
var N = 100 ; function count ( n , c , a , b ) { var need = Array ( N ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } var s = 0 ; var tank = 0 ; for ( var i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = Math . min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; var ans = 1 ; need [ s + n ] = 0 ; for ( var i = 1 ; i < n ; i ++ ) { var id = s + n - i ; need [ id ] = Math . max ( 0 , need [ id + 1 ] + b [ id ] - Math . min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } var n = 3 ; var c = 3 ; var a = [ 3 , 1 , 2 ] ; var b = [ 2 , 2 , 2 ] ; document . write ( count ( n , c , a , b ) ) ;