find pair of numbers in array that add to given sum

前端 未结 19 2189
萌比男神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:28

    Ruby implementation

    ar1 = [ 32, 44, 68, 54, 65, 43, 68, 46, 68, 56]
    for i in 0..ar1.length-1
     t  = 100 - ar1[i]
      if ar1.include?(t)
        s= ar1.count(t)
        if s < 2
          print   s , " - " ,  t, " , " , ar1[i]  , " pair ", i, "\n"
        end
      end
    end
    

提交回复
热议问题