We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In Python 3, I recommend to use a dictionary to counts the number of repetitions for each integer.
As many people have told, the number of possible different pairs if a number is repeated N times is N*(N-1), not over 2 as the combinatory coefficient nCr is, because one must count twice, i.e. (0,1) and (1,0).
defsolve(a):# Write your code herea_dic={}pairs=0#loop to count the number of elementsforiinrange(len(a)):ifa[i]notina_dic:a_dic[a[i]]=0#{a[i]:0}a_dic[a[i]]+=1#{a[i]:+1}addone#loop for check whats integers taht are more than onceforkina_dic:val=a_dic[k]ifval>1:#conditionmorethanoncepairs+=val*(val-1)#possiblepairsn*(n-1)returnpairs
Sherlock and Pairs
You are viewing a single comment's thread. Return to all comments →
In Python 3, I recommend to use a dictionary to counts the number of repetitions for each integer. As many people have told, the number of possible different pairs if a number is repeated N times is N*(N-1), not over 2 as the combinatory coefficient nCr is, because one must count twice, i.e. (0,1) and (1,0).