Question: Javascript Pair Sum At Most K Given an array of integer numbers and a target sum, locate a pair of two numbers in the array

Javascript

Pair Sum At Most K

Given an array of integer numbers and a target sum, locate a pair of two numbers in the array which together produce the maximum sum that does not exceed the target.

Your result should be an array of two numbers sorted ascending. If there are no numbers that produce a sum less than or equal to the target, return an array of [-1, -1] to indicate failure. If there are multiple pairs that produce the maximum sum, return any pair you wish.

0 nums length 106

0 nums[i] 106

0 target 106

Examples

Example 1

const nums = [6, 4, 2, 3, 8]; const target = 13; pairSumOrLess(nums, target); // => [4, 8] 

Here, the best we can do is the pair [4, 8], which approach the target of 13 as closely as possible but do not exceed it.

Example 2

const nums = [7, 2, 4, 9, 1, 13]; const target = 13; pairSumOrLess(nums, target); // => [4, 9] 

In this example, there exist two numbers in the array which sum to the target precisely.

Example 3

const nums = [7, 2, 4, 9, 1, 13]; const target = 2; pairSumOrLess(nums, target); // => [-1, -1] 

No pairs of numbers in the array are less than or equal to the target of 2. We reject this array by returning [-1, -1].

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!