{"id":9256,"date":"2021-12-26T06:04:34","date_gmt":"2021-12-26T14:04:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9256"},"modified":"2021-12-26T06:10:17","modified_gmt":"2021-12-26T14:10:17","slug":"leetcode-2122-recover-the-original-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2122-recover-the-original-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2122. Recover the Original Array"},"content":{"rendered":"\n<p>Alice had a&nbsp;<strong>0-indexed<\/strong>&nbsp;array&nbsp;<code>arr<\/code>&nbsp;consisting of&nbsp;<code>n<\/code>&nbsp;<strong>positive<\/strong>&nbsp;integers. She chose an arbitrary&nbsp;<strong>positive integer<\/strong>&nbsp;<code>k<\/code>&nbsp;and created two new&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays&nbsp;<code>lower<\/code>&nbsp;and&nbsp;<code>higher<\/code>&nbsp;in the following manner:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>lower[i] = arr[i] - k<\/code>, for every index&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt; n<\/code><\/li><li><code>higher[i] = arr[i] + k<\/code>, for every index&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt; n<\/code><\/li><\/ol>\n\n\n\n<p>Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays&nbsp;<code>lower<\/code>&nbsp;and&nbsp;<code>higher<\/code>, but not the array each integer belonged to. Help Alice and recover the original array.<\/p>\n\n\n\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;consisting of&nbsp;<code>2n<\/code>&nbsp;integers, where&nbsp;<strong>exactly<\/strong>&nbsp;<code>n<\/code>&nbsp;of the integers were present in&nbsp;<code>lower<\/code>&nbsp;and the remaining in&nbsp;<code>higher<\/code>, return&nbsp;<em>the&nbsp;<strong>original<\/strong>&nbsp;array<\/em>&nbsp;<code>arr<\/code>. In case the answer is not unique, return&nbsp;<em><strong>any<\/strong>&nbsp;valid array<\/em>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The test cases are generated such that there exists&nbsp;<strong>at least one<\/strong>&nbsp;valid array&nbsp;<code>arr<\/code>.<\/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 = [2,10,6,4,8,12]\n<strong>Output:<\/strong> [3,7,11]\n<strong>Explanation:<\/strong>\nIf arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].\nCombining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.\nAnother valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12]. \n<\/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,1,3,3]\n<strong>Output:<\/strong> [2,2]\n<strong>Explanation:<\/strong>\nIf arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].\nCombining lower and higher gives us [1,1,3,3], which is equal to nums.\nNote that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.\nThis is invalid since k must be positive.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,435]\n<strong>Output:<\/strong> [220]\n<strong>Explanation:<\/strong>\nThe only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 * n == nums.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li>The test cases are generated such that there exists&nbsp;<strong>at least one<\/strong>&nbsp;valid array&nbsp;<code>arr<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Try all possible k<\/strong><\/h2>\n\n\n\n<p>Sort the array, we know that the smallest number nums[0] is org[0] &#8211; k, org[0] + k (nums[0] + 2k) must exist in nums. We try all possible ks. k = (nums[i] &#8211; nums[0]) \/ 2.<\/p>\n\n\n\n<p>Then we iterate the sorted nums array as low, and see whether we can find low + 2k as high using a dynamic hashtable.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n)<\/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  vector<int> recoverArray(vector<int>& nums) {\n    const int n = nums.size();\n    sort(begin(nums), end(nums));\n    unordered_map<int, int> m;\n    for (int x : nums) ++m[x];\n    \n    auto check = [&](int k) -> vector<int> {      \n      vector<int> ans;\n      unordered_map<int, int> cur(m);\n      for (int x : nums) {\n        if (cur[x] == 0) continue;\n        --cur[x];\n        if (--cur[x + 2 * k] < 0) return {};\n        ans.push_back(x + k);\n      }\n      return ans;\n    };\n    for (int i = 1; i < n; ++i) {\n      if (nums[i] == nums[0] || (nums[i] - nums[0]) &#038; 1) continue;\n      const int k = (nums[i] - nums[0]) \/ 2;\n      vector<int> ans = check(k);\n      if (!ans.empty()) return ans;\n    }\n    return {};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Alice had a&nbsp;0-indexed&nbsp;array&nbsp;arr&nbsp;consisting of&nbsp;n&nbsp;positive&nbsp;integers. She chose an arbitrary&nbsp;positive integer&nbsp;k&nbsp;and created two new&nbsp;0-indexed&nbsp;integer arrays&nbsp;lower&nbsp;and&nbsp;higher&nbsp;in the following manner: lower[i] = arr[i] &#8211; k, for every index&nbsp;i&nbsp;where&nbsp;0 &lt;=&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[217,82,31],"class_list":["post-9256","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hard","tag-hashtable","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9256","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=9256"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9256\/revisions"}],"predecessor-version":[{"id":9260,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9256\/revisions\/9260"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}