From 18cf392f7d7058f45fbc026d9dd44a7df714d28a Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sat, 8 Oct 2022 14:22:54 +0530 Subject: [PATCH] Added Minimize Maximum Pair Sum in Array - Added Minimize Maximum Pair Sum in Array and corresponding tests. --- .../Minimize_Maximum_Pair_Sum_In_Array.js | 46 +++++++++++++++++++ ...Minimize_Maximum_Pair_Sum_In_Array_Test.js | 10 ++++ README.md | 1 + 3 files changed, 57 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js create mode 100644 LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js diff --git a/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js b/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js new file mode 100644 index 0000000..75671a3 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js @@ -0,0 +1,46 @@ +/* +Minimize Maximum Pair Sum in Array +https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/description/ + +The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs. + +For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8. +Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that: + +Each element of nums is in exactly one pair, and +The maximum pair sum is minimized. +Return the minimized maximum pair sum after optimally pairing up the elements. + + + +Example 1: + +Input: nums = [3,5,2,3] +Output: 7 +Explanation: The elements can be paired up into pairs (3,3) and (5,2). +The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7. + + +Example 2: + +Input: nums = [3,5,4,2,4,6] +Output: 8 +Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2). +The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8. +*/ + +/** + * @param {number[]} nums + * @return {number} + */ + var minPairSum = function(nums) { + nums.sort((a, b) => a-b); + let i = 0, j = nums.length - 1; + let max = -Infinity; + while (i < j) { + max = Math.max(max, nums[i++] + nums[j--]); + } + return max; +}; + +module.exports.minPairSum = minPairSum; diff --git a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js new file mode 100644 index 0000000..240edd6 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js @@ -0,0 +1,10 @@ +const assert = require('assert'); +const minPairSum = require('../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array').minPairSum; + +var test = function () { + assert.equal(minPairSum([3,5,2,3]), 7); + assert.equal(minPairSum([3,5,4,2,4,6]), 8); + assert.equal(minPairSum([1,1,1,1]), 2); +} + +module.exports.test = test; diff --git a/README.md b/README.md index ac9c66a..91d495b 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ To run a specific problem in your console run `node ` (e.g. | [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ | | [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ | | [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ | +| [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |