{"id":9470,"date":"2022-02-04T17:01:40","date_gmt":"2022-02-05T01:01:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9470"},"modified":"2022-02-04T17:06:04","modified_gmt":"2022-02-05T01:06:04","slug":"leetcode-2149-rearrange-array-elements-by-sign","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-2149-rearrange-array-elements-by-sign\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2149. Rearrange Array Elements by Sign"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<strong>even<\/strong>&nbsp;length consisting of an&nbsp;<strong>equal<\/strong>&nbsp;number of positive and negative integers.<\/p>\n\n\n\n<p>You should&nbsp;<strong>rearrange<\/strong>&nbsp;the elements of&nbsp;<code>nums<\/code>&nbsp;such that the modified array follows the given conditions:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Every&nbsp;<strong>consecutive pair<\/strong>&nbsp;of integers have&nbsp;<strong>opposite signs<\/strong>.<\/li><li>For all integers with the same sign, the&nbsp;<strong>order<\/strong>&nbsp;in which they were present in&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>preserved<\/strong>.<\/li><li>The rearranged array begins with a positive integer.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the modified array after rearranging the elements to satisfy the aforementioned conditions<\/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 = [3,1,-2,-5,2,-4]\n<strong>Output:<\/strong> [3,-2,1,-5,2,-4]\n<strong>Explanation:<\/strong>\nThe positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].\nThe only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].\nOther ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more 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,1]\n<strong>Output:<\/strong> [1,-1]\n<strong>Explanation:<\/strong>\n1 is the only positive integer and -1 the only negative integer in nums.\nSo nums is rearranged to [1,-1].\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;= 2 * 10<sup>5<\/sup><\/code><\/li><li><code>nums.length<\/code>&nbsp;is&nbsp;<strong>even<\/strong><\/li><li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>nums<\/code>&nbsp;consists of&nbsp;<strong>equal<\/strong>&nbsp;number of positive and negative integers.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Split and merge<\/strong><\/h2>\n\n\n\n<p>Create two arrays to store positive and negative numbers.<\/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  vector<int> rearrangeArray(vector<int>& nums) {\n    vector<int> pos;\n    vector<int> neg;\n    for (int x : nums)\n      (x > 0 ? pos : neg).push_back(x);\n    vector<int> ans;\n    for (int i = 0; i < pos.size(); ++i) {\n      ans.push_back(pos[i]);\n      ans.push_back(neg[i]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Use two pointers to store the next pos \/ neg.<\/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  vector<int> rearrangeArray(vector<int>& nums) {\n    vector<int> ans(nums.size());    \n    int pos = 0;\n    int neg = 1;\n    for (int x : nums) {\n      int& index = x > 0 ? pos : neg;\n      ans[index] = x;\n      index += 2;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;of&nbsp;even&nbsp;length consisting of an&nbsp;equal&nbsp;number of positive and negative integers. You should&nbsp;rearrange&nbsp;the elements of&nbsp;nums&nbsp;such that the modified array follows the given conditions:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[20,177,762,175],"class_list":["post-9470","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-array","tag-medium","tag-rearrange","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9470","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=9470"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9470\/revisions"}],"predecessor-version":[{"id":9473,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9470\/revisions\/9473"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}