Two sum: Difference between revisions

Content added Content deleted
Line 3,040: Line 3,040:
=={{header|V (Vlang)}}==
=={{header|V (Vlang)}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn two_sum(a []int, target_sum int) (int, int, bool) {
<syntaxhighlight lang="v (vlang)">
fn two_sum(a []int, target_sum int) (int, int, bool) {
len := a.len
len := a.len
if len < 2 {
if len < 2 {return 0, 0, false}
return 0, 0, false
}
for i in 0..len - 1 {
for i in 0..len - 1 {
if a[i] <= target_sum {
if a[i] <= target_sum {
for j in i + 1..len {
for j in i + 1..len {
sum := a[i] + a[j]
sum := a[i] + a[j]
if sum == target_sum {
if sum == target_sum {return i, j, true}
return i, j, true
if sum > target_sum {break}
}
if sum > target_sum {
break
}
}
}
} else {
}
break
else {break}
}
}
}
return 0, 0, false
return 0, 0, false
Line 3,067: Line 3,061:
target_sum := 21
target_sum := 21
p1, p2, ok := two_sum(a, target_sum)
p1, p2, ok := two_sum(a, target_sum)
if !ok {println("No two numbers were found whose sum is $target_sum")}
if !ok {
println("No two numbers were found whose sum is $target_sum")
else {println("The numbers with indices $p1 and $p2 sum to $target_sum")}
}
} else {
</syntaxhighlight>
println("The numbers with indices $p1 and $p2 sum to $target_sum")
}
}</syntaxhighlight>


{{out}}
{{out}}