The code finds the maximum value of an array A, using a for-loop. The maximum value is found at position i, inclusive, and is the largest value of A[i] + A[j] that is not greater than K.
function solution(A, K) {
let max = 0;
for (let i = 0; i < A.length; i++) {
for (let j = i + 1; j < A.length; j++) {
if (A[i] + A[j] < K) {
max = Math.max(max, A[i] + A[j]);
}
}
}
return max === 0 ? -1 : max;
}