{"id":9707,"date":"2022-05-01T21:49:42","date_gmt":"2022-05-02T04:49:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9707"},"modified":"2022-05-01T21:50:24","modified_gmt":"2022-05-02T04:50:24","slug":"leetcode-2256-minimum-average-difference","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2256-minimum-average-difference\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2256. Minimum Average Difference"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>average difference<\/strong>&nbsp;of the index&nbsp;<code>i<\/code>&nbsp;is the&nbsp;<strong>absolute<\/strong>&nbsp;<strong>difference<\/strong>&nbsp;between the average of the&nbsp;<strong>first<\/strong>&nbsp;<code>i + 1<\/code>&nbsp;elements of&nbsp;<code>nums<\/code>&nbsp;and the average of the&nbsp;<strong>last<\/strong>&nbsp;<code>n - i - 1<\/code>&nbsp;elements. Both averages should be&nbsp;<strong>rounded down<\/strong>&nbsp;to the nearest integer.<\/p>\n\n\n\n<p>Return<em>&nbsp;the index with the&nbsp;<strong>minimum average difference<\/strong><\/em>. If there are multiple such indices, return the&nbsp;<strong>smallest<\/strong>&nbsp;one.<\/p>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<strong>absolute difference<\/strong>&nbsp;of two numbers is the absolute value of their difference.<\/li><li>The&nbsp;<strong>average<\/strong>&nbsp;of&nbsp;<code>n<\/code>&nbsp;elements is the&nbsp;<strong>sum<\/strong>&nbsp;of the&nbsp;<code>n<\/code>&nbsp;elements divided (<strong>integer division<\/strong>) by&nbsp;<code>n<\/code>.<\/li><li>The average of&nbsp;<code>0<\/code>&nbsp;elements is considered to be&nbsp;<code>0<\/code>.<\/li><\/ul>\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,5,3,9,5,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\n- The average difference of index 0 is: |2 \/ 1 - (5 + 3 + 9 + 5 + 3) \/ 5| = |2 \/ 1 - 25 \/ 5| = |2 - 5| = 3.\n- The average difference of index 1 is: |(2 + 5) \/ 2 - (3 + 9 + 5 + 3) \/ 4| = |7 \/ 2 - 20 \/ 4| = |3 - 5| = 2.\n- The average difference of index 2 is: |(2 + 5 + 3) \/ 3 - (9 + 5 + 3) \/ 3| = |10 \/ 3 - 17 \/ 3| = |3 - 5| = 2.\n- The average difference of index 3 is: |(2 + 5 + 3 + 9) \/ 4 - (5 + 3) \/ 2| = |19 \/ 4 - 8 \/ 2| = |4 - 4| = 0.\n- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) \/ 5 - 3 \/ 1| = |24 \/ 5 - 3 \/ 1| = |4 - 3| = 1.\n- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) \/ 6 - 0| = |27 \/ 6 - 0| = |4 - 0| = 4.\nThe average difference of index 3 is the minimum average difference so return 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> nums = [0]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>\nThe only index is 0 so return 0.\nThe average difference of index 0 is: |0 \/ 1 - 0| = |0 - 0| = 0.\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>0 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>Solution: Prefix \/ Suffix Sum<\/p>\n\n\n\n<p>Time complexity: O(n)<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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumAverageDifference(vector<int>& nums) {\n    const int n = nums.size();\n    long long suffix = accumulate(begin(nums), end(nums), 0LL);\n    long long prefix = 0;\n    int best = INT_MAX;\n    int ans = 0;\n    for (int i = 0; i < n; ++i) {\n      prefix += nums[i];\n      suffix -= nums[i];\n      int cur = abs(prefix \/ (i + 1) - (i == n - 1 ? 0 : suffix \/ (n - i - 1)));\n      if (cur < best) {\n        best = cur;\n        ans = i;\n      }\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 length&nbsp;n. The&nbsp;average difference&nbsp;of the index&nbsp;i&nbsp;is the&nbsp;absolute&nbsp;difference&nbsp;between the average of the&nbsp;first&nbsp;i + 1&nbsp;elements of&nbsp;nums&nbsp;and the average of the&nbsp;last&nbsp;n &#8211; i -&#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,97,186],"class_list":["post-9707","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-prefix","tag-suffix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9707","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=9707"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9707\/revisions"}],"predecessor-version":[{"id":9709,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9707\/revisions\/9709"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}