{"id":7702,"date":"2020-11-22T14:47:12","date_gmt":"2020-11-22T22:47:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7702"},"modified":"2020-11-22T14:48:22","modified_gmt":"2020-11-22T22:48:22","slug":"leetcode-1664-ways-to-make-a-fair-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1664-ways-to-make-a-fair-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1664. Ways to Make a Fair Array"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>. You can choose&nbsp;<strong>exactly one<\/strong>&nbsp;index (<strong>0-indexed<\/strong>) and remove the element. Notice that the index of the elements may change after the removal.<\/p>\n\n\n\n<p>For example, if&nbsp;<code>nums = [6,1,7,4,1]<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choosing to remove index&nbsp;<code>1<\/code>&nbsp;results in&nbsp;<code>nums = [6,7,4,1]<\/code>.<\/li><li>Choosing to remove index&nbsp;<code>2<\/code>&nbsp;results in&nbsp;<code>nums = [6,1,4,1]<\/code>.<\/li><li>Choosing to remove index&nbsp;<code>4<\/code>&nbsp;results in&nbsp;<code>nums = [6,1,7,4]<\/code>.<\/li><\/ul>\n\n\n\n<p>An array is&nbsp;<strong>fair<\/strong>&nbsp;if the sum of the odd-indexed values equals the sum of the even-indexed values.<\/p>\n\n\n\n<p>Return the&nbsp;<em><strong>number<\/strong>&nbsp;of indices that you could choose such that after the removal,&nbsp;<\/em><code>nums<\/code><em>is&nbsp;<strong>fair<\/strong>.<\/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 = [2,1,6,4]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nRemove index 0: [1,6,4] -&gt; Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.\nRemove index 1: [2,6,4] -&gt; Even sum: 2 + 4 = 6. Odd sum: 6. Fair.\nRemove index 2: [2,1,4] -&gt; Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.\nRemove index 3: [2,1,6] -&gt; Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.\nThere is 1 index that you can remove to make nums fair.\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,1]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>&nbsp;You can remove any index and the remaining array is fair.\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 = [1,2,3]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>&nbsp;You cannot make a fair array after removing any index.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/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: Prefix Sum<\/strong><\/h2>\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  int waysToMakeFair(vector<int>& nums) {\n    int n = nums.size();\n    vector<int> odds(n + 1);\n    vector<int> evens(n + 1);\n    for (int i = 0; i < n; ++i) {      \n      odds[i + 1] = odds[i] + (i % 2 == 1) * nums[i];\n      evens[i + 1] = evens[i] + (i % 2 == 0) * nums[i];      \n    }\n    int ans = 0;\n    for (int i = 0; i < n; ++i) {\n      const int odd = odds[i] + (evens[n] - evens[i + 1]);\n      const int even = evens[i] + (odds[n] - odds[i + 1]);\n      ans += odd == even;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums. You can choose&nbsp;exactly one&nbsp;index (0-indexed) and remove the element. Notice that the index of the elements may change after&#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,177,200],"class_list":["post-7702","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7702","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=7702"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7702\/revisions"}],"predecessor-version":[{"id":7704,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7702\/revisions\/7704"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}