+ 1

# Why am i getting the wrong answer for this hacker rank excercise?

# Complete the sockMerchant function below. def sockMerchant(n,ar): ar.sort() count, element, element2 = 0, ar[::2], ar[1::2] combine = list(zip(element,element2)) for t in combine: if t[0] == t[1]: count += 1 return count So basically what this code is doing is taking a list of numbers and counting the pairs that exist in the list. for eg: (5,[1,1,1,1,2]). The pairs (1,1) and (1,1) would make the answer be two. The 5 just represents how many elements are in the list. All of the test cases work properly in hacker rank except for this one: (20,[4, 5, 5, 5 ,6 ,6 ,4 ,1 ,4 ,4, 3, 6 ,6 ,3, 6, 1, 4 ,5 ,5, 5]) and I don't know why. The answer should be 9 but I get 23 instead. Does anyone know why this is happening?

2 Answers

+ 4

Result is 8
It compares pair of alternate positions in the sorted list.
https://code.sololearn.com/cvlew8iJTIO7/?ref=app

0

The answer is indeed 8 but i don't know why hacker rank expects me to get 9 for the answer@Gordon