Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value
if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; }