|
| 1 | +<h2><a href="https://leetcode.com/problems/absolute-difference-between-maximum-and-minimum-k-elements">4158. Absolute Difference Between Maximum and Minimum K Elements</a></h2><h3>Easy</h3><hr><p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>Find the absolute difference between:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>the <strong>sum</strong> of the <code>k</code> <strong>largest</strong> elements in the array; and</li> |
| 7 | + <li>the <strong>sum</strong> of the <code>k</code> <strong>smallest</strong> elements in the array.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return an integer denoting this difference.</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<div class="example-block"> |
| 16 | +<p><strong>Input:</strong> <span class="example-io">nums = [5,2,2,4], k = 2</span></p> |
| 17 | + |
| 18 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 19 | + |
| 20 | +<p><strong>Explanation:</strong></p> |
| 21 | + |
| 22 | +<ul> |
| 23 | + <li>The <code>k = 2</code> largest elements are 4 and 5. Their sum is <code>4 + 5 = 9</code>.</li> |
| 24 | + <li>The <code>k = 2</code> smallest elements are 2 and 2. Their sum is <code>2 + 2 = 4</code>.</li> |
| 25 | + <li>The absolute difference is <code>abs(9 - 4) = 5</code>.</li> |
| 26 | +</ul> |
| 27 | +</div> |
| 28 | + |
| 29 | +<p><strong class="example">Example 2:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>Input:</strong> <span class="example-io">nums = [100], k = 1</span></p> |
| 33 | + |
| 34 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 35 | + |
| 36 | +<p><strong>Explanation:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li>The largest element is 100.</li> |
| 40 | + <li>The smallest element is 100.</li> |
| 41 | + <li>The absolute difference is <code>abs(100 - 100) = 0</code>.</li> |
| 42 | +</ul> |
| 43 | +</div> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= n == nums.length <= 100</code></li> |
| 50 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 51 | + <li><code>1 <= k <= n</code></li> |
| 52 | +</ul> |
0 commit comments