Pair Sum At Most K Given an array of integer numbers and a target sum, locate a pair of…
- User
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].
solve it in javasdcript node 18.x
const pairSumOrLess = (nums, target) => {
};
- Assistant
const pairSumOrLess = (nums, target) => { let result = [-1, -1]; let maxSum = -1; nums.sort((a, b) => a - b);
let left = 0; let right = nums.length - 1;
while (left < right) { const sum = nums[left] + nums[right];
if (sum <= target) { if (sum > maxSum) { maxSum = sum; result = [nums[left], nums[right]]; } left++; } else { right--; }}
return result; };
// Example 1 const nums1 = [6, 4, 2, 3, 8]; const target1 = 13; console.log(pairSumOrLess(nums1, target1)); // => [4, 8]
// Example 2 const nums2 = [7, 2, 4, 9, 1, 13]; const target2 = 13; console.log(pairSumOrLess(nums2, target2)); // => [4, 9]
// Example 3 const nums3 = [7, 2, 4, 9, 1, 13]; const target3 = 2; console.log(pairSumOrLess(nums3, target3)); // => [-1, -1]
- User
For this challenge you will be using some meta-programming magic to create a new Thing object. This object will allow you to define things in a descriptive sentence like format.
This challenge will build on itself in an increasingly complex manner as you progress through the test cases.
Examples of what can be done with "Thing": Note: Most of the test cases have already been provided for you so that you can see how the Thing object is supposed to work.
class Thing { // TODO: Make the magic happen }
solve in javascript node 18.x
- Assistant
Here's an implementation of the
Thingclass that allows you to define things in a descriptive sentence-like format:class Thing { constructor(name) { this.name = name; this.properties = {}; } isA(type) { this.type = type; return this; } hasA(propertyName) { return { of: (propertyValue) => { this.properties[propertyName] = propertyValue; return this; }, }; } says(str) { this.say = () => str; return this; } } const cat = new Thing("cat") .isA("animal") .hasA("sound") .of("meow") .says("Cats are the best pets."); const dog = new Thing("dog") .isA("animal") .hasA("sound") .of("bark") .says("Dogs are good for guarding homes."); console.log(cat.name); // "cat" console.log(cat.type); // "animal" console.log(cat.properties.sound); // "meow" console.log(cat.say()); // "Cats are the best pets." console.log(dog.name); // "dog" console.log(dog.type); // "animal" console.log(dog.properties.sound); // "bark" console.log(dog.say()); // "Dogs are good for guarding homes."This implementation provides a way to chain methods for defining properties of a thing, such as
isAfor types,hasAandoffor defining property values, andsaysfor specifying a custom say method. - User
help me fix the previous code, this is the output
TypeError: jane.has is not a function at Suite. (/workspace/node/test.js:76:12) at Object.create (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/common.js:148:19) at context.describe.context.context (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/bdd.js:42:27) at Suite. (/workspace/node/test.js:74:5) at Object.create (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/common.js:148:19) at context.describe.context.context (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/bdd.js:42:27) at Suite. (/workspace/node/test.js:73:3) at Object.create (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/common.js:148:19) at context.describe.context.context (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/interfaces/bdd.js:42:27) at /workspace/node/test.js:45:1 at Object. (/workspace/node/test.js:199:3) at Module._compile (node:internal/modules/cjs/loader:1159:14) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at ModuleWrap. (node:internal/modules/esm/translators:169:29) at ModuleJob.run (node:internal/modules/esm/module_job:193:25) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:530:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15) at async formattedImport (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:9:14) at async exports.requireOrImport (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:42:28) at async exports.loadFilesAsync (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:100:20) at async singleRun (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/cli/run-helpers.js:125:3) at async exports.handler (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/cli/run.js:370:5)
- Assistant
I apologize for the confusion. In my previous response, I didn't include the
hasmethod in theThingclass. This is why you see a TypeError sayingjane.hasis not a function. Let's fix that by adding the missinghasmethod:class Thing { constructor(name) { this.name = name; this.properties = {}; } isA(type) { this.type = type; return this; } hasA(propertyName) { return { of: (propertyValue) => { this.properties[propertyName] = propertyValue; return this; }, }; } has(propertyName) { return this.hasA(propertyName); } says(str) { this.say = () => str; return this; } } const cat = new Thing("cat") .isA("animal") .hasA("sound") .of("meow") .says("Cats are the best pets."); const dog = new Thing("dog") .isA("animal") .has("sound") .of("bark") .says("Dogs are good for guarding homes."); console.log(cat.name); // "cat" console.log(cat.type); // "animal" console.log(cat.properties.sound); // "meow" console.log(cat.say()); // "Cats are the best pets." console.log(dog.name); // "dog" console.log(dog.type); // "animal" console.log(dog.properties.sound); // "bark" console.log(dog.say()); // "Dogs are good for guarding homes."Now, the
Thingclass has ahasmethod that works the same as thehasAmethod, and you should no longer see the TypeError message.