From 116240cf06b3d92895e539d6743cad790a714134 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Fri, 6 Jan 2023 23:58:09 -0800 Subject: [PATCH 1/3] solve 2441. Largest Positive Integer That Exists With Its Negative --- ...e-integer-that-exists-with-its-negative.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2441.largest-positive-integer-that-exists-with-its-negative.js diff --git a/2441.largest-positive-integer-that-exists-with-its-negative.js b/2441.largest-positive-integer-that-exists-with-its-negative.js new file mode 100644 index 00000000..eccc3f20 --- /dev/null +++ b/2441.largest-positive-integer-that-exists-with-its-negative.js @@ -0,0 +1,19 @@ +/* URL of this problem + * https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/description/ + * + * @param {number[]} nums + * @return {number} + */ + +var findMaxK = function(nums) { + const Sorted = [...nums].sort((a, b) => b - a); + + for (let i = 0; i < Sorted.length; i++) { + if (Sorted.includes(-1 * Sorted[i])) { + return Sorted[i]; + } + } + return -1; +}; + +module.exports = findMaxK; \ No newline at end of file From 63dfc61fc4e01b2c494cb26c9586da3c7cc2a0fc Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Sat, 7 Jan 2023 15:53:52 -0800 Subject: [PATCH 2/3] test function findMaxK --- findMaxK.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 findMaxK.test.js diff --git a/findMaxK.test.js b/findMaxK.test.js new file mode 100644 index 00000000..4484b18c --- /dev/null +++ b/findMaxK.test.js @@ -0,0 +1,17 @@ +const findMaxK = require("./2441.largest-positive-integer-that-exists-with-its-negative"); + +test("Return the largest positive integer k such that -k also exists in the array", () => { + expect(findMaxK([-1,2,-3,3])).toBe(3); +}); + +test("Return -1 if there is no such a positive integer k that -k also exists in the array", () => { + expect(findMaxK([-10,8,6,7,-2,-3])).toBe(-1); +}); + +test("Return Infinity if the array contains Infinity and -Infinity", () => { + expect(findMaxK([Infinity, -Infinity])).toBe(Infinity); +}); + +test("Return 0 if the array contains only zeroes", () => { + expect(findMaxK([0,0])).toBe(0); +}); \ No newline at end of file From f1c9d178784899c0e72d4b2e99aa48a5e32b4cdd Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Sat, 7 Jan 2023 15:58:14 -0800 Subject: [PATCH 3/3] add a comment and fix some code --- ...rgest-positive-integer-that-exists-with-its-negative.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/2441.largest-positive-integer-that-exists-with-its-negative.js b/2441.largest-positive-integer-that-exists-with-its-negative.js index eccc3f20..543aa871 100644 --- a/2441.largest-positive-integer-that-exists-with-its-negative.js +++ b/2441.largest-positive-integer-that-exists-with-its-negative.js @@ -6,11 +6,14 @@ */ var findMaxK = function(nums) { + // Sort the input nums in a descending order const Sorted = [...nums].sort((a, b) => b - a); for (let i = 0; i < Sorted.length; i++) { - if (Sorted.includes(-1 * Sorted[i])) { - return Sorted[i]; + const Num = Sorted[i]; + + if (Sorted.includes(Num * -1)) { + return Num; } } return -1;