Two sum: Difference between revisions

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