{"id":7250,"date":"2020-08-15T21:59:32","date_gmt":"2020-08-16T04:59:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7250"},"modified":"2020-08-15T23:46:21","modified_gmt":"2020-08-16T06:46:21","slug":"leetcode-1551-minimum-operations-to-make-array-equal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1551-minimum-operations-to-make-array-equal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1551. Minimum Operations to Make Array Equal"},"content":{"rendered":"\n<p>You have an array&nbsp;<code>arr<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>arr[i] = (2 * i) + 1<\/code>&nbsp;for all valid values of&nbsp;<code>i<\/code>&nbsp;(i.e.&nbsp;<code>0 &lt;= i &lt; n<\/code>).<\/p>\n\n\n\n<p>In one operation, you can select two indices&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;where&nbsp;<code>0 &lt;= x, y &lt; n<\/code>&nbsp;and subtract&nbsp;<code>1<\/code>&nbsp;from&nbsp;<code>arr[x]<\/code>&nbsp;and add&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>arr[y]<\/code>&nbsp;(i.e. perform&nbsp;<code>arr[x] -=1&nbsp;<\/code>and&nbsp;<code>arr[y] += 1<\/code>).&nbsp;The goal is to make all the elements of the array&nbsp;<strong>equal<\/strong>. It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that all the elements of the array can be made equal using some operations.<\/p>\n\n\n\n<p>Given an integer&nbsp;<code>n<\/code>, the length of the array. Return&nbsp;<em>the minimum number of operations<\/em>&nbsp;needed to make&nbsp;all the elements of arr equal.<\/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> n = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> arr = [1, 3, 5]\nFirst operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]\nIn the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 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> n = 6\n<strong>Output:<\/strong> 9\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>1: Find the mean (final value) of the array, assuming x, easy to show x == n<br>2: Compute the sum of an arithmetic progression of (x &#8211; 1) + (x &#8211; 3) + &#8230; for n \/\/ 2 pairs<br><br>e.g. n = 6<br>arr = [1, 3, 5, 7, 9, 11]<br>x = (1 + 2 * n &#8211; 1) \/ 2 = 6 = n<br>steps = (6 &#8211; 1) + (6 &#8211; 3) + (6 &#8211; 5) = (n \/\/ 2) * (n &#8211; (1 + n &#8211; 1) \/ 2) = (n \/\/ 2) * (n &#8211; n \/\/ 2) = 3 * 3 = 9<br><br>e.g. n = 5<br>arr = [1,3,5,7,9]<br>x = (1 + 2 * n  &#8211; 1) \/ 2 = 5 = n<br>steps = (5 &#8211; 1) + (5 &#8211; 3)= (n\/\/2) * (n &#8211; n \/\/ 2) = (n \/\/ 2) * ((n + 1) \/\/ 2) = 2 * 3 = 6<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  int minOperations(int n) {    \n    return (n \/ 2) * ((n + 1) \/ 2);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have an array&nbsp;arr&nbsp;of length&nbsp;n&nbsp;where&nbsp;arr[i] = (2 * i) + 1&nbsp;for all valid values of&nbsp;i&nbsp;(i.e.&nbsp;0 &lt;= i &lt; n). In one operation, you can select&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-7250","post","type-post","status-publish","format-standard","hentry","category-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7250","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=7250"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7250\/revisions"}],"predecessor-version":[{"id":7259,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7250\/revisions\/7259"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}