educative.io

Assumption on the solution


#1

input: [5, 3, 1, 3, 0, 9]
target: 5

isn’t the output for this input is 4? the solution gave 6. Is it because the input might have multiple solutions and the program returns triplet with smallest sum?

Thank you


#2

Hi Chris, you are absolutely right. Using their approach, if the array is sorted, we would come across the triplet 0, 1, 5 before 0, 1, 3 and so the solution is 0, 1, 5 according to their code. Their solution does not account for the triplet with smallest sum when the absolute distance between the target and multiple triplets are the same.


#3

I see. hmm, it confused me for a while hahaha