Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums and an integer k.
Find the absolute difference between:
k largest elements in the array; andk smallest elements in the array.Return an integer denoting this difference.
Example 1:
Input: nums = [5,2,2,4], k = 2
Output: 5
Explanation:
k = 2 largest elements are 4 and 5. Their sum is 4 + 5 = 9.k = 2 smallest elements are 2 and 2. Their sum is 2 + 2 = 4.abs(9 - 4) = 5.Example 2:
Input: nums = [100], k = 1
Output: 0
Explanation:
abs(100 - 100) = 0.Constraints:
1 <= n == nums.length <= 1001 <= nums[i] <= 1001 <= k <= nProblem summary: You are given an integer array nums and an integer k. Find the absolute difference between: the sum of the k largest elements in the array; and the sum of the k smallest elements in the array. Return an integer denoting this difference.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[5,2,2,4] 2
[100] 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
class Solution {
public int absDifference(int[] nums, int k) {
Arrays.sort(nums);
int ans = 0;
int n = nums.length;
for (int i = 0; i < k; ++i) {
ans += nums[n - i - 1] - nums[i];
}
return ans;
}
}
// Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
func absDifference(nums []int, k int) (ans int) {
slices.Sort(nums)
for i := 0; i < k; i++ {
ans += nums[len(nums)-i-1] - nums[i]
}
return
}
# Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
class Solution:
def absDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return sum(nums[-k:]) - sum(nums[:k])
// Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
// class Solution {
// public int absDifference(int[] nums, int k) {
// Arrays.sort(nums);
// int ans = 0;
// int n = nums.length;
// for (int i = 0; i < k; ++i) {
// ans += nums[n - i - 1] - nums[i];
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3774: Absolute Difference Between Maximum and Minimum K Elements
function absDifference(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < k; ++i) {
ans += nums.at(-i - 1)! - nums[i];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.