Find pair with sum Ruby Algorithm

Wendyharris
Jan 17, 2021

Given an array A[] and a number x, check for pair in A[] with sum as x

— Approach 1: Using Hash Map

def check_pair(arr,x)

len=arr.length

flag=false

map=Hash.new()

for i in 0…len

t=x-arr[i]

if map[t]

flag=true

break

end

map[arr[i]]=1

end

if flag

return “Array has two elements with sum:#{x}”

else

return “Array doesn’t have two elements with sum:#{x}”

end

end

check_pair([5,4,10,-2,3,2,-1,9],9)

— Approach 2: Sorting and then searching using left and right indexes.

def check_pair(arr,x)

len=arr.length

arr.sort!

left=0

right=len-1

while left<right

if (arr[left]+arr[right]==x)

return “Array has two elements with sum:#{x}”

elsif (arr[left]+arr[right]>x)

right-=1

else

left+=1

end

end

return “Array doesn’t have two elements with sum:#{x}”

end

check_pair([5,4,10,-2,3,2,-1,9],9)

--

--