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 Overview: Given an integer array and an integer k, compute the absolute difference between the k-th smallest element and the k-th largest element. The task reduces to identifying order statistics in the array and measuring the distance between those two values.
Approach 1: Sorting (O(n log n) time, O(1) or O(n) space)
Sort the array in non‑decreasing order using a standard sorting algorithm. After sorting, the k‑th smallest element sits at index k-1 and the k‑th largest element sits at index n-k. The answer becomes abs(nums[n-k] - nums[k-1]). This works because sorting places all values in rank order, so positional indexing directly gives the required order statistics. The implementation is simple and reliable, making it the most practical solution for this problem.
Sorting also integrates cleanly with standard library functions in most languages. You perform one sort operation, read the two positions, and compute the difference. Since arrays are already the underlying data structure, no additional structures are required beyond what the sort implementation uses internally. This makes it a natural solution when working with arrays and rank‑based queries.
Approach 2: Selection (Quickselect / Heap) (Average O(n) time, O(1)–O(k) space)
Instead of fully sorting the array, you can compute order statistics directly. Algorithms like Quickselect find the k‑th smallest element in average O(n) time. Run it twice: once to find the k‑th smallest value and once to find the k‑th largest (or equivalently the (n-k+1)-th smallest). Another alternative is using two heaps: a min‑heap for the largest k elements and a max‑heap for the smallest k elements. These methods avoid sorting the entire dataset and focus only on the elements needed for the calculation.
This approach becomes useful when the array is very large and only a few order statistics are required. It leverages selection algorithms rather than full sorting, reducing average runtime to linear time. The tradeoff is slightly more complex code and edge‑case handling compared to the straightforward sort solution.
Recommended for interviews: The sorting approach is usually expected. It shows clear reasoning and produces an O(n log n) solution with minimal code. Mentioning Quickselect as a possible O(n) optimization demonstrates deeper understanding, but implementing sorting first communicates correctness and clarity under interview time constraints.
We first sort the array nums. Then we calculate the sum of the first k elements and the sum of the last k elements in the array, and finally return the difference between them.
The time complexity is O(n times log n), and the space complexity is O(log n), where n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting | O(n log n) | O(1) to O(n) | Best general solution. Simple implementation using built‑in sort. |
| Quickselect (Order Statistics) | O(n) average | O(1) | When only k‑th smallest and k‑th largest values are required. |
| Heap Based Selection | O(n log k) | O(k) | Useful for streaming data or when maintaining top/bottom k elements. |
3774. Absolute Difference Between Maximum and Minimum K Elements (Leetcode Easy) • Programming Live with Larry • 195 views views
Watch 5 more video solutions →Practice Absolute Difference Between Maximum and Minimum K Elements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor