Find if possible to visit every nodes in given Graph exactly once based on given conditions | Java program for the above approach ; Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static void findpath ( int N , int a [ ] ) { if ( a [ 0 ] == 1 ) { System . out . print ( ( N + 1 ) + " ▁ " ) ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( ( i ) + " ▁ " ) ; return ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( a [ i ] == 0 && a [ i + 1 ] == 1 ) { for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( ( j ) + " ▁ " ) ; System . out . print ( ( N + 1 ) + " ▁ " ) ; for ( int j = i + 1 ; j <= N ; j ++ ) System . out . print ( ( j ) + " ▁ " ) ; return ; } } for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( ( i ) + " ▁ " ) ; System . out . print ( ( N + 1 ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 3 , arr [ ] = { 0 , 1 , 0 } ; int N = 3 , arr [ ] = { 0 , 1 , 0 } ; findpath ( N , arr ) ; } }