3774. Absolute Difference Between Maximum and Minimum K Elements Easy
1/**
2 * [3774] Absolute Difference Between Maximum and Minimum K Elements
3 *
4 * You are given an integer array nums and an integer k.
5 * Find the absolute difference between:
6 *
7 * the sum of the k largest elements in the array; and
8 * the sum of the k smallest elements in the array.
9 *
10 * Return an integer denoting this difference.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [5,2,2,4], k = 2</span>
15 * Output: <span class="example-io">5</span>
16 * Explanation:
17 *
18 * The k = 2 largest elements are 4 and 5. Their sum is 4 + 5 = 9.
19 * The k = 2 smallest elements are 2 and 2. Their sum is 2 + 2 = 4.
20 * The absolute difference is abs(9 - 4) = 5.
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [100], k = 1</span>
25 * Output: <span class="example-io">0</span>
26 * Explanation:
27 *
28 * The largest element is 100.
29 * The smallest element is 100.
30 * The absolute difference is abs(100 - 100) = 0.
31 * </div>
32 *
33 * Constraints:
34 *
35 * 1 <= n == nums.length <= 100
36 * 1 <= nums[i] <= 100
37 * 1 <= k <= n
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/absolute-difference-between-maximum-and-minimum-k-elements/
43// discuss: https://leetcode.com/problems/absolute-difference-between-maximum-and-minimum-k-elements/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn abs_difference(nums: Vec<i32>, k: i32) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3774() {
61 }
62}
63Back
© 2026 bowen.ge All Rights Reserved.