union based on rank
if ( rank [ root_a ] < rank [ root_b ] ) : NEW_LINE INDENT arr [ root_a ] = arr [ root_b ] NEW_LINE rank [ root_b ] += rank [ root_a ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ root_b ] = arr [ root_a ] NEW_LINE rank [ root_a ] += rank [ root_b ] NEW_LINE DEDENT