{"id":9577,"date":"2022-03-21T04:25:29","date_gmt":"2022-03-21T11:25:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9577"},"modified":"2022-03-21T04:26:35","modified_gmt":"2022-03-21T11:26:35","slug":"leetcode-2206-divide-array-into-equal-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2206-divide-array-into-equal-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2206. Divide Array Into Equal Pairs"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;consisting of&nbsp;<code>2 * n<\/code>&nbsp;integers.<\/p>\n\n\n\n<p>You need to divide&nbsp;<code>nums<\/code>&nbsp;into&nbsp;<code>n<\/code>&nbsp;pairs such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each element belongs to&nbsp;<strong>exactly one<\/strong>&nbsp;pair.<\/li><li>The elements present in a pair are&nbsp;<strong>equal<\/strong>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if nums can be divided into<\/em>&nbsp;<code>n<\/code>&nbsp;<em>pairs, otherwise return<\/em>&nbsp;<code>false<\/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 = [3,2,3,2,2,2]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> \nThere are 6 elements in nums, so they should be divided into 6 \/ 2 = 3 pairs.\nIf nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.\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,2,3,4]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> \nThere is no way to divide nums into 4 \/ 2 = 2 pairs such that the pairs satisfy every condition.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums.length == 2 * n<\/code><\/li><li><code>1 &lt;= n &lt;= 500<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 500<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Each number has to appear even numbers in order to be paired. Count the frequency of each number, return true if all of them are even numbers, return false otherwise. <\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool divideArray(vector<int>& nums) {\n    unordered_map<int, int> m;\n    for (int num : nums)\n      ++m[num];\n    return all_of(begin(m), end(m), [](const auto& e) { return e.second % 2 == 0; });    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;consisting of&nbsp;2 * n&nbsp;integers. You need to divide&nbsp;nums&nbsp;into&nbsp;n&nbsp;pairs such that: Each element belongs to&nbsp;exactly one&nbsp;pair. The elements present in a&#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":[254,222,82],"class_list":["post-9577","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-counter","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9577","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=9577"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9577\/revisions"}],"predecessor-version":[{"id":9579,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9577\/revisions\/9579"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}