Number of circular tours that visit all petrol pumps | Java 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
import java . io . * ; class GFG { static int N = 100 ; public static int count ( int n , int c , int a [ ] , int b [ ] ) { int need [ ] = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } int s = 0 ; int tank = 0 ; for ( int 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 ; int ans = 1 ; need [ s + n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int 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 ; } public static void main ( String args [ ] ) { int n = 3 ; int c = 3 ; int [ ] a = new int [ ] { 3 , 1 , 2 , 0 , 0 , 0 } ; int [ ] b = new int [ ] { 2 , 2 , 2 , 0 , 0 , 0 } ; System . out . print ( count ( n , c , a , b ) + "NEW_LINE"); } }