for (i in 1..n-2) { j = ii+1 // Start whereright iafter isi. k = n // Start at the end of the array. while (k >= j) { // We got a match! All done. if (A[i] + A[j] + A[k] == 0) return (A[i], A[j], A[k]) // We didn't match. Let's try to get a little closer: // If the sum was too big, decrement k. // If the sum was too small, increment j. (A[i] + A[j] + A[k] > 0) ? k-- : j++ } // When the while-loop finishes, j and k have passed each other and there's // no more useful combinations that we can try with this i. } Jirka
- 4.2k
- 32
- 40
added 290 characters in body; added 57 characters in body; added 19 characters in body; added 16 characters in body
John Feminella
- 313.3k
- 49
- 348
- 362