my code passes all test cases except
pairwise([0, 0, 0, 0, 1, 1], 1)
and I know why it doesn’t, I just don’t understand how that is supposed to equal 10 and not 4.
Wouldn’t the smallest sum of indices be [0] + [4]?
You forgot about the second pair that adds up to 1. Their indexes are 1 and 5.
20 characters of text
You’re correct . You’ll use the smallest pair of indexes in that case.
20 characters of text
Oh, sorry, I didn’t read your post well…
I don’t think so. There are two such pairs: 0 4
and 1 5
, which add up to 10
.
The test has four 0
s and two 1
s (with 1
as target sum), so there’s a lot of ways you can pair up a 0
and a 1
. However the pair with the smallest indexes has indexes 0
and 4
.
Then you proceed to look up more pairs. There are three 0
s and a 1
remaining. Again there are many ways to pair these up, but the pair with the smallest indexes has indexes 1
and 5
.
Then you proceed to look up more pairs. There are two 0
s remaining. There’s no way these can be paired up, so you conclude that you have all the indexes you need: 0 4 1 5
. You add these up and come up with 10
.
20 characters of text
For what it’s worth: you are not the only one with a different interpretation. Mine was exactly the same and it worked as well as yours, though with a different implementation.
The description is unnecessarily muddled IMO. It would have sufficed to say that once an index has been used it cannot be used again.
“If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices.”
By that description the result should be 4 not 10. But in this case they consider the elements by their indices, not their numerical value since 0, 4 and 1, 5 both have (0, 1) and (0,1) which are identical numerical values but they use different indices.
There is no “smallest sum of indices”. If so - which indices? The ones that repeat? If you mark off numerical values that have already been used then 10 is incorrect. If you go by indices that have already been used then 10 is correct but there is no smallest sum of indices as there will always be unique indices to be added.
The confusion is that most people solved this by using the “ticking off” type of algorithm where indices that are found to be a match have their values set to NaN or something else so that they can’t be used again but if the same numerical values appear in other indices then they are valid to be used again because the indices are different.
More proof of this confusion: in the explanation of the challenge they have this:
Remember to return the smaller sum if multiple are possible. This means ([1,1,1], 1) should use 0 + 1 instead of 0 + 1 & 1 + 1.
This doesn’t make sense if you use their algorithm there will never be a comparison of two similar indices (1, 1) since the i, j comparison always has an offset of 1 (j = i + 1)
Agree with @animakuz, this test case is wrong. There is no way this does what the exercise is intended to do.
Thanks for clarfying. but why would you not consider the 0 and 5. That also sums upto 1 correct? The question is def a twister, but am not clear what the question is trying to resolve. There is some ambiguity.
If I recall correctly, you pair the smallest possible indexes. While 0 5
is a possible pair, 0 4
is smaller, so that pair is what’s used.