{"id":7961,"date":"2021-01-10T09:47:03","date_gmt":"2021-01-10T17:47:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7961"},"modified":"2021-01-10T09:47:31","modified_gmt":"2021-01-10T17:47:31","slug":"leetcode-1720-decode-xored-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1720-decode-xored-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1720. Decode XORed Array"},"content":{"rendered":"\n<p>There is a&nbsp;<strong>hidden<\/strong>&nbsp;integer array&nbsp;<code>arr<\/code>&nbsp;that consists of&nbsp;<code>n<\/code>&nbsp;non-negative integers.<\/p>\n\n\n\n<p>It was encoded into another integer array&nbsp;<code>encoded<\/code>&nbsp;of length&nbsp;<code>n - 1<\/code>, such that&nbsp;<code>encoded[i] = arr[i] XOR arr[i + 1]<\/code>. For example, if&nbsp;<code>arr = [1,0,2,1]<\/code>, then&nbsp;<code>encoded = [1,2,3]<\/code>.<\/p>\n\n\n\n<p>You are given the&nbsp;<code>encoded<\/code>&nbsp;array. You are also given an integer&nbsp;<code>first<\/code>, that is the first element of&nbsp;<code>arr<\/code>, i.e.&nbsp;<code>arr[0]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the original array<\/em>&nbsp;<code>arr<\/code>. It can be proved that the answer exists and is unique.<\/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> encoded = [1,2,3], first = 1\n<strong>Output:<\/strong> [1,0,2,1]\n<strong>Explanation:<\/strong> If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]\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> encoded = [6,2,7,3], first = 4\n<strong>Output:<\/strong> [4,2,0,7,4]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>encoded.length == n - 1<\/code><\/li><li><code>0 &lt;= encoded[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= first &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: XOR<\/strong><\/h2>\n\n\n\n<p>encoded[i] = arr[i] ^ arr[i + 1]<br>encoded[i] ^ arr[i] = arr[i] ^ arr[i] ^ arr[i + 1]<br>arr[i+1] = encoded[i]^arr[i]<\/p>\n\n\n\n<p>Time complexity: O(n)<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++\">\nclass Solution {\npublic:\n  vector<int> decode(vector<int>& encoded, int first) {\n    const int n = encoded.size() + 1;\n    vector<int> ans(n, first);\n    for (int i = 0; i + 1 < n; ++i)\n      ans[i + 1] = ans[i] ^ encoded[i];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a&nbsp;hidden&nbsp;integer array&nbsp;arr&nbsp;that consists of&nbsp;n&nbsp;non-negative integers. It was encoded into another integer array&nbsp;encoded&nbsp;of length&nbsp;n &#8211; 1, such that&nbsp;encoded[i] = arr[i] XOR arr[i + 1].&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,222,63],"class_list":["post-7961","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7961","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=7961"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7961\/revisions"}],"predecessor-version":[{"id":7963,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7961\/revisions\/7963"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}