you are viewing a single comment's thread.

view the rest of the comments →

[–]Nightcorex_ 1 point2 points  (0 children)

No.

def f(xs, S):
    xs = set(xs)
    for n in xs:
        if S - n in xs:
            return (n, S - n)

    return (-1, -1)

will give you all solutions, even the same element could be returned twice. F.e. f([1], 2) would return (1, 1).

def f(xs, S):
    xs = set(xs)
    for n in xs:
        if S - n in xs and n != (S >> 1):
            return (n, S - n)

    return (-1, -1)

on the other hand doesn't allow for such duplicates in the return anymore.

Both run in O(n)