Pairing problem doubt


sir it is giving WA please check

Hello @Shivanchal-Agarwal-2530139037020445, I think you got confused with the logic or went in the wrong direction. You just need to do the dfs and find out the diff components and store the size of the components into the vector or array.
Now suppose the array arr has a1,a2,a3…ak elements (suppose k diff components we have)
now we need every pair from this array like
a1*a2 + a1*a3 + a1*a4… a1*ak +
a2*a3 + a2*a4 + a2*a5… a2*ak +… and so on
then we can think of like the formula for getting these pairs
then can be find out using the identity (a1+a2+a3…an)^2 = a1^2 + a2^2 + a3^2 + a4^2 … an^2 + 2*(a1*a2+a1*a3…)
now 1/2( (a1+a2+a3…an)^2 - (a1^2 + a2^2 + a3^2 + a4^2 … an^2 ))
So may be it seems to be complicated but it is not. You just need to find out the distinct component size and then apply this formula.
I hope it will be clear to you. In case you feel some confusion pls let me know.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.