Two sum: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(added X86 Assembly)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 339:
[1,3]
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
 
public class Program
{
public static void Main(string[] args)
{
int[] arr = { 0, 2, 11, 19, 90 };
const int sum = 21;
 
var ts = TwoSum(arr, sum);
Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");
 
Console.ReadLine();
}
 
public static int[] TwoSum(int[] numbers, int sum)
{
var map = new Dictionary<int, int>();
for (int i = 0; i < numbers.Length; i++)
{
// see if the complement is stored
var key = sum - numbers[i];
if (map.ContainsKey(key))
{
return new[] { map[key], i };
}
map.Add(numbers[i], i);
}
return null;
}
}
</lang>
{{out}}
<pre>1, 3</pre>
 
=={{header|C++}}==
Line 379 ⟶ 416:
{{out}}
<pre>{1,3}</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
 
public class Program
{
public static void Main(string[] args)
{
int[] arr = { 0, 2, 11, 19, 90 };
const int sum = 21;
 
var ts = TwoSum(arr, sum);
Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");
 
Console.ReadLine();
}
 
public static int[] TwoSum(int[] numbers, int sum)
{
var map = new Dictionary<int, int>();
for (int i = 0; i < numbers.Length; i++)
{
// see if the complement is stored
var key = sum - numbers[i];
if (map.ContainsKey(key))
{
return new[] { map[key], i };
}
map.Add(numbers[i], i);
}
return null;
}
}
</lang>
{{out}}
<pre>1, 3</pre>
 
=={{header|D}}==
Line 665:
The numbers with indices 1 and 3 sum to 21
</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
Line 956 ⟶ 957:
}</lang>
<pre>[1, 3]</pre>
 
 
=={{header|JavaScript}}==
Line 1,334:
[1,3]
</pre>
 
 
=={{header|OCaml}}==
Line 1,544 ⟶ 1,543:
<pre>1, 3
No match</pre>
 
=={{header|Perl 6}}==
 
===Procedural===
{{trans|zkl}}
<lang perl6>sub two_sum ( @numbers, $sum ) {
die '@numbers is not sorted' unless [<=] @numbers;
 
my ( $i, $j ) = 0, @numbers.end;
while $i < $j {
given $sum <=> @numbers[$i,$j].sum {
when Order::More { $i += 1 }
when Order::Less { $j -= 1 }
when Order::Same { return $i, $j }
}
}
return;
}
 
say two_sum ( 0, 2, 11, 19, 90 ), 21;
say two_sum ( 0, 2, 11, 19, 90 ), 25;</lang>
{{out}}
<pre>(1 3)
Nil</pre>
 
===Functional===
The two versions differ only in how one 'reads' the notional flow of processing: left-to-right versus right-to-left.
Both return all pairs that sum to the target value, not just the first (e.g. for input of <code>0 2 10 11 19 90</code> would get indices 1/4 and 2/3).
<lang perl6>sub two-sum-lr (@a, $sum) {
# (((^@a X ^@a) Z=> (@a X+ @a)).grep($sum == *.value)>>.keys.map:{ .split(' ').sort.join(' ')}).unique
(
(
(^@a X ^@a) Z=> (@a X+ @a)
).grep($sum == *.value)>>.keys
.map:{ .split(' ').sort.join(' ')}
).unique
}
 
sub two-sum-rl (@a, $sum) {
# unique map {.split(' ').sort.join(' ')}, keys %(grep {.value == $sum}, ((^@a X ^@a) Z=> (@a X+ @a)))
unique
map {.split(' ').sort.join(' ')},
keys %(
grep {.value == $sum}, (
(^@a X ^@a) Z=> (@a X+ @a)
)
)
}
 
my @a = <0 2 11 19 90>;
for 21, 25 {
say two-sum-rl(@a, $_);
say two-sum-lr(@a, $_);
}</lang>
{{out}}
<pre>(1 3)
(1 3)
()
()</pre>
 
=={{header|Phix}}==
Line 1,888 ⟶ 1,828:
(check-equal? (two-sum #(-3 -2 0 1 5 8 11) 17) #f)
(check-equal? (two-sum #(-8 -2 -1 1 5 9 11) 0) '(2 3)))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
===Procedural===
{{trans|zkl}}
<lang perl6>sub two_sum ( @numbers, $sum ) {
die '@numbers is not sorted' unless [<=] @numbers;
 
my ( $i, $j ) = 0, @numbers.end;
while $i < $j {
given $sum <=> @numbers[$i,$j].sum {
when Order::More { $i += 1 }
when Order::Less { $j -= 1 }
when Order::Same { return $i, $j }
}
}
return;
}
 
say two_sum ( 0, 2, 11, 19, 90 ), 21;
say two_sum ( 0, 2, 11, 19, 90 ), 25;</lang>
{{out}}
<pre>(1 3)
Nil</pre>
 
===Functional===
The two versions differ only in how one 'reads' the notional flow of processing: left-to-right versus right-to-left.
Both return all pairs that sum to the target value, not just the first (e.g. for input of <code>0 2 10 11 19 90</code> would get indices 1/4 and 2/3).
<lang perl6>sub two-sum-lr (@a, $sum) {
# (((^@a X ^@a) Z=> (@a X+ @a)).grep($sum == *.value)>>.keys.map:{ .split(' ').sort.join(' ')}).unique
(
(
(^@a X ^@a) Z=> (@a X+ @a)
).grep($sum == *.value)>>.keys
.map:{ .split(' ').sort.join(' ')}
).unique
}
 
sub two-sum-rl (@a, $sum) {
# unique map {.split(' ').sort.join(' ')}, keys %(grep {.value == $sum}, ((^@a X ^@a) Z=> (@a X+ @a)))
unique
map {.split(' ').sort.join(' ')},
keys %(
grep {.value == $sum}, (
(^@a X ^@a) Z=> (@a X+ @a)
)
)
}
 
my @a = <0 2 11 19 90>;
for 21, 25 {
say two-sum-rl(@a, $_);
say two-sum-lr(@a, $_);
}</lang>
{{out}}
<pre>(1 3)
(1 3)
()
()</pre>
 
=={{header|REXX}}==
Line 2,094:
Some((1, 3))
</pre>
 
=={{header|Scala}}==
<lang Scala>import java.util
 
object TwoSum extends App {
val (sum, arr)= (21, Array(0, 2, 11, 19, 90))
println(util.Arrays.toString(twoSum(arr, sum)))
 
private def twoSum(a: Array[Int], target: Long): Array[Int] = {
var (i, j) = (0, a.length - 1)
while (i < j) {
val sum = a(i) + a(j)
if (sum == target) return Array[Int](i, j)
if (sum < target) i += 1 else j -= 1
}
null
}
 
}</lang>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/GxVfCE7/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/S5aks2gRTcqcy1VUWJ6GzQ Scastie (JVM)].
 
=={{header|Sidef}}==
Line 2,116 ⟶ 2,136:
[]
</pre>
=={{header|Scala}}==
<lang Scala>import java.util
 
object TwoSum extends App {
val (sum, arr)= (21, Array(0, 2, 11, 19, 90))
println(util.Arrays.toString(twoSum(arr, sum)))
 
private def twoSum(a: Array[Int], target: Long): Array[Int] = {
var (i, j) = (0, a.length - 1)
while (i < j) {
val sum = a(i) + a(j)
if (sum == target) return Array[Int](i, j)
if (sum < target) i += 1 else j -= 1
}
null
}
 
}</lang>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/GxVfCE7/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/S5aks2gRTcqcy1VUWJ6GzQ Scastie (JVM)].
=={{header|Stata}}==
Notice that array indexes start at 1 in Stata.
10,333

edits