If last node is reached and it has a link to the starting node i . e the source then keep the minimum value out of the total cost of traversal and " ans " Finally return to check for more possible values
if ( count == n && graph [ currPos ] [ 0 ] ) { ans = Math . min ( ans , cost + graph [ currPos ] [ 0 ] ) ; return ; }