Number of circular tours that visit all petrol pumps | Python 3 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 . ; Driver Code
N = 100 NEW_LINE def count ( n , c , a , b ) : NEW_LINE INDENT need = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT a [ i + n ] = a [ i ] NEW_LINE b [ i + n ] = b [ i ] NEW_LINE DEDENT s = 0 NEW_LINE tank = 0 NEW_LINE for i in range ( 0 , 2 * n , 1 ) : NEW_LINE INDENT tank += a [ i ] NEW_LINE tank = min ( tank , c ) NEW_LINE tank -= b [ i ] NEW_LINE if ( tank < 0 ) : NEW_LINE INDENT tank = 0 NEW_LINE s = i + 1 NEW_LINE DEDENT DEDENT if ( s >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 1 NEW_LINE need [ s + n ] = 0 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT id = s + n - i NEW_LINE need [ id ] = max ( 0 , need [ id + 1 ] + b [ id ] - min ( a [ id ] , c ) ) NEW_LINE if ( need [ id ] == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE c = 3 NEW_LINE a = [ 3 , 1 , 2 , 0 , 0 , 0 ] NEW_LINE b = [ 2 , 2 , 2 , 0 , 0 , 0 ] NEW_LINE print ( count ( n , c , a , b ) ) NEW_LINE DEDENT