My code is showing wrong answer plz check my code and correct my code and let me know my mistake.----- problem : https://online.codingblocks.com/app/player/39658/content/77399/5256/code-challenge ------Mycode : https://ide.codingblocks.com/s/221145 plz reply Asap
Wrong answer for pairing question in graph algorithms
long long int ans = 0;
for (int i = 0; i < ans_vec.size() - 1; i++)
{
for (int j = i + 1; j < ans_vec.size(); j++)
{
ans += (ans_vec[i] * ans_vec[j]);
}
}
return ans;
Why you you think this will give the answer…??
Shouldn’t the answer be product of sizes of all connected components…??
I am first calculating the number of nodes in each component ans store it in the vector and then generating pairs
Ex: for 3 components
first has nodes 1,2,3 no. of nodes=3
second has nodes 4,5 no. of nodes=2
third has 2 nodes 7,8 no. of nodes =2
the pairs can be between first and second component = 32=6 ,between first third=32=6 and between second and third = 2*2=4
total=6+6 4=16.
This is the logic which i am using
why 322?
we have to form pairs so we can choose any two sets at a time.
please reply on my above reply.
Yes, I am sorry, 3x2x2 is wrong. and a better way of ding the same would be C(n,2)-sum(C(ni,2))
where C(n,r) = n choose r, and n is the tot number of vertices, ni is the number of vertices in ith connected comp, and sum(ai) means sum of all ai
Why my code is showing wrong answer plz check my approach?
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.