{"id":10067,"date":"2023-08-13T14:40:33","date_gmt":"2023-08-13T21:40:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10067"},"modified":"2023-08-13T14:41:17","modified_gmt":"2023-08-13T21:41:17","slug":"leetcode-2815-max-pair-sum-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2815-max-pair-sum-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2815. Max Pair Sum in an Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>. You have to find the&nbsp;<strong>maximum<\/strong>&nbsp;sum of a pair of numbers from&nbsp;<code>nums<\/code>&nbsp;such that the maximum&nbsp;<strong>digit&nbsp;<\/strong>in both numbers are equal.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum sum or<\/em>&nbsp;<code>-1<\/code><em>&nbsp;if no such pair exists<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [51,71,17,24,42]\n<strong>Output:<\/strong> 88\n<strong>Explanation:<\/strong> \nFor i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. \nFor i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.\nIt can be shown that there are no other pairs with equal maximum digits, so the answer is 88.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> No pair exists in nums with equal maximum digits.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate all pairs of nums and check their sum and max digit.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxSum(vector<int>& nums) {\n    auto maxDigit = [](int x) {\n      int ans = 0;\n      while (x) {\n        ans = max(ans, x % 10);\n        x \/= 10;\n      }\n      return ans;\n    };\n    int ans = -1;\n    const int n = nums.size();\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        if (nums[i] + nums[j] > ans \n            && maxDigit(nums[i]) == maxDigit(nums[j]))\n          ans = nums[i] + nums[j];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums. You have to find the&nbsp;maximum&nbsp;sum of a pair of numbers from&nbsp;nums&nbsp;such that the maximum&nbsp;digit&nbsp;in both numbers are equal. Return&nbsp;the maximum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,157,222,148],"class_list":["post-10067","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-digit","tag-easy","tag-pair","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10067"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10067\/revisions"}],"predecessor-version":[{"id":10073,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10067\/revisions\/10073"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}