Find if possible to visit every nodes in given Graph exactly once based on given conditions | 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 ; Given Input ; Function Call
function findpath ( N , a ) { if ( a [ 0 ] ) { document . write ( N + 1 ) ; for ( let i = 1 ; i <= N ; i ++ ) document . write ( i ) ; return ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( ! a [ i ] && a [ i + 1 ] ) { for ( let j = 1 ; j <= i ; j ++ ) document . write ( j + " " ) ; document . write ( N + 1 + " " ) ; for ( let j = i + 1 ; j <= N ; j ++ ) document . write ( j + " " ) ; return ; } } for ( let i = 1 ; i <= N ; i ++ ) document . write ( i + " " ) ; document . write ( N + 1 + " " ) ; } let N = 3 , arr = [ 0 , 1 , 0 ] ; findpath ( N , arr ) ;