Find if possible to visit every nodes in given Graph exactly once based on given conditions | C ++ program for 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findpath ( int N , int a [ ] ) { if ( a [ 0 ] ) { cout << " ▁ " << N + 1 ; for ( int i = 1 ; i <= N ; i ++ ) cout << " ▁ " << i ; return ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ! a [ i ] && a [ i + 1 ] ) { for ( int j = 1 ; j <= i ; j ++ ) cout << " ▁ " << j ; cout << " ▁ " << N + 1 ; for ( int j = i + 1 ; j <= N ; j ++ ) cout << " ▁ " << j ; return ; } } for ( int i = 1 ; i <= N ; i ++ ) cout << " ▁ " << i ; cout << " ▁ " << N + 1 ; } int main ( ) { int N = 3 , arr [ ] = { 0 , 1 , 0 } ; findpath ( N , arr ) ; }