you wil be given an array integer, and number, you should to return the sum of number in (a,b) example : (1,2,3,6,7,8,9,1) and the number is 10 the output is = (1,9),(2,8),(3,7),(8,2),(9,1),(9,1)
function sumpairs(input, k)
map = Dict{Int,Int}()
count = 0
for n in input
complement = k - input[n]
compl_freq = get(map, complement, 0)
if compl_freq != 0
count += 1
if compl_freq == 1
delete!(map, complement)
else
map[complement] -= 1
end
else
map[n] = get(map, n, 0) + 1
end
end
return count
end
sumpairs([1,2,3,4,5,6,1,2],6)
i wrote the code in julia, but it doesn't work, can some one explain why? and if its wrote right ?
k
and the code must be wrapped inside afunction
declaration. Did you write this yourself? Can you provide the entire code? - DNFinput
andk
both as input arguments and as constants inside the function. And thereturn
statement is in the wrong place. Can you try to clean this up, and then post an example showing how you call it and what output you get? - DNF2
. I don't think that's what you want, though. Why are you returningcount
instead ofmap
? You need to understand the algorithm if you're going to solve this problem. - DNF