find pair of numbers in array that add to given sum

前端 未结 19 2152
萌比男神i
萌比男神i 2020-11-30 20:17

Question: Given an unsorted array of positive integers, is it possible to find a pair of integers from that array that sum up to a given sum?

Constraints: This shou

19条回答
  •  执笔经年
    2020-11-30 20:27

    def pair_sum(arr,k):
        counter = 0
        lookup = set()
        for num in arr:
            if k-num in lookup:
                counter+=1
            else:
                lookup.add(num)
        return counter
        pass
    pair_sum([1,3,2,2],4)
    

    The solution in python

提交回复
热议问题