Problem

Given an array A of integers and integer K, return the maximum S such that there exist i < j with A[i] + A[j] = S and S < K.
If no i, j exist satisfying this equation, return -1

Test array A = [34, 23, 1, 24, 75, 33, 54, 8], K = 60.

Solution

The solution to the problem is shown in Figures 1, 2 and 3 on this page. The code is written in PHP. Figures 1 – 2 show the code. Figure 3 shows the output.

Figure 1. Problem code page 1
Figure 2. Problem code page 2
Figure 3. Problem code output

Feel free to leave your comments below. I will respond. Thank you.

NB: The full code to the problem can be seen on Github at: https://github.com/ugogineering/PHP-codes/blob/main/two-sums-less-than-k.php

2 replies on “Sums of two integers in an array less than K”

Comments are closed.