|
| 1 | +<h2><a href="https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/"><div id="big-omega-company-tags"><div id="big-omega-topbar"><div class="companyTagsContainer" style="overflow-x: scroll; flex-wrap: nowrap;"><div class="companyTagsContainer--tag">No companies found for this problem</div></div><div class="companyTagsContainer--chevron"><div><svg version="1.1" id="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 32 32" fill="#4087F1" xml:space="preserve" style="width: 20px;"><polygon points="16,22 6,12 7.4,10.6 16,19.2 24.6,10.6 26,12 "></polygon><rect id="_x3C_Transparent_Rectangle_x3E_" class="st0" fill="none" width="32" height="32"></rect></svg></div></div></div></div>1685. Sum of Absolute Differences in a Sorted Array</a></h2><h3>Medium</h3><hr><div><p>You are given an integer array <code>nums</code> sorted in <strong>non-decreasing</strong> order.</p> |
| 2 | + |
| 3 | +<p>Build and return <em>an integer array </em><code>result</code><em> with the same length as </em><code>nums</code><em> such that </em><code>result[i]</code><em> is equal to the <strong>summation of absolute differences</strong> between </em><code>nums[i]</code><em> and all the other elements in the array.</em></p> |
| 4 | + |
| 5 | +<p>In other words, <code>result[i]</code> is equal to <code>sum(|nums[i]-nums[j]|)</code> where <code>0 <= j < nums.length</code> and <code>j != i</code> (<strong>0-indexed</strong>).</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> nums = [2,3,5] |
| 11 | +<strong>Output:</strong> [4,3,5] |
| 12 | +<strong>Explanation:</strong> Assuming the arrays are 0-indexed, then |
| 13 | +result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4, |
| 14 | +result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3, |
| 15 | +result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5. |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> nums = [1,4,6,8,10] |
| 21 | +<strong>Output:</strong> [24,15,13,15,21] |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Constraints:</strong></p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li><code>2 <= nums.length <= 10<sup>5</sup></code></li> |
| 29 | + <li><code>1 <= nums[i] <= nums[i + 1] <= 10<sup>4</sup></code></li> |
| 30 | +</ul> |
| 31 | +</div> |
0 commit comments