I don't know how to make my code more efficient. It completes the examples, but it will time out when it tackles the big array.
I ask you for some advice for performance on loops, or if you can tell me where my error is. If my approach is completely wrong don't give me the answer please.
What i have to do: Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum.
Examples:
sum_pairs([11, 3, 7, 5], 10) => [3, 7]
sum_pairs([4, 3, 2, 3, 4], 6)=>[2, 4],[3, 3],[2, 4]
(indices:0,2|1,3|2,4)=>
- entire pair is earlier, and therefore is the correct answer===[2, 4]
sum_pairs([0, 0, -2, 3], 2) there are no pairs of values that can be added to produce 2. == None/nil/undefined (Based on the language)
sum_pairs([10, 5, 2, 3, 7, 5],10)=>[5, 5][3, 7]
--------------------1------------5
--------------------------3--4
indicies:(1,5|3,4)
- entire pair is earlier, and therefore is the correct answer == [3, 7]
My attemt:
var sum_pairs=function(ints, s){
for(t=1; t <=ints.length-1;t++){
for(i=0; i <=ints.length-t-1;i++){
if(ints[i]+ints[i+t]===s){
return [ints[i],ints[i+t]];
}
}
}
}