Two sum: Difference between revisions

Content added Content deleted
(add RPL)
Line 1,055: Line 1,055:
i = 1
i = 1
j = len array[]
j = len array[]
# The array remains empty if no sum is found
pair[] = [ ]
pair[] = [ ]
repeat
repeat
if array[i] + array[j] = sum
if array[i] + array[j] = sum
pair[] = [ i j ]
pair[] = [ i j ]
break 2
return
elif array[i] + array[j] > sum
elif array[i] + array[j] > sum
j -= 1
j -= 1
Line 1,070: Line 1,069:
.
.
numbers[] = [ 0 2 11 19 90 ]
numbers[] = [ 0 2 11 19 90 ]
call twoSum 21 numbers[] pair[]
twoSum 21 numbers[] pair[]
print "[" & pair[1] & ", " & pair[2] & "]"
print pair[]
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>[2, 4]</pre>
<pre>[ 2 4]</pre>


=={{header|Elixir}}==
=={{header|Elixir}}==