Advertisement
zvoulgaris

2sum

Mar 11th, 2020
3,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.57 KB | None | 0 0
  1. # 2sum drill
  2.  
  3. function main(x::Array{Int64, 1}, y::Int64)
  4.     n = length(x)
  5.     z = Array{Int64}(undef, n, 2)
  6.     h = Array{UInt64}(undef, n)
  7.     c = 0
  8.  
  9.     for i = 1:n
  10.         xx = y - x[i]
  11.  
  12.         if xx in x
  13.             temp = [x[i], xx]
  14.             temp_ = [xx, x[i]]
  15.  
  16.             if !(hash(temp) in h[1:c]) && !(hash(temp_) in h[1:c])
  17.                 c += 1
  18.                 z[c,:] = temp
  19.                 h[c] = hash(temp)
  20.             end
  21.         end
  22.     end
  23.  
  24.     if c > 0
  25.         return z[1:c,:]
  26.     else
  27.         println("no pairs found!")
  28.     end
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement