Two sum: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,056: | Line 1,056: | ||
main()</lang> |
main()</lang> |
||
=={{header|Objeck}}== |
|||
{{trans|Java}} |
|||
<lang objeck>class TwoSum { |
|||
function : Main(args : String[]) ~ Nil { |
|||
sum := 21; |
|||
arr := [0, 2, 11, 19, 90]; |
|||
Print(TwoSum(arr, sum)); |
|||
} |
|||
function : TwoSum(a : Int[], target : Int) ~ Int[] { |
|||
i := 0; |
|||
j := a->Size() - 1; |
|||
while (i < j) { |
|||
sum := a[i] + a[j]; |
|||
if(sum = target) { |
|||
r := Int->New[2]; |
|||
r[0] := i; |
|||
r[1] := j; |
|||
return r; |
|||
}; |
|||
if (sum < target) { |
|||
i++; |
|||
} |
|||
else { |
|||
j--; |
|||
}; |
|||
}; |
|||
return Nil; |
|||
} |
|||
function : Print(r : Int[]) ~ Nil { |
|||
'['->Print(); |
|||
each(i : r) { |
|||
r[i]->Print(); |
|||
if(i + 1 < r->Size()) { |
|||
','->Print(); |
|||
}; |
|||
}; |
|||
']'->PrintLine(); |
|||
} |
|||
} |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
[1,3] |
|||
</pre> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |