Absolute Difference Between Maximum and Minimum K Elements - Solution & Explanation
Problem Statement
You are given an integer array nums and an integer k.
Find the absolute difference between:
- the sum of the
klargest elements in the array; and - the sum of the
ksmallest elements in the array.
Return an integer denoting this difference.
Example 1:
Input: nums = [5,2,2,4], k = 2
Output: 5
Explanation:
- The
k = 2largest elements are 4 and 5. Their sum is4 + 5 = 9. - The
k = 2smallest elements are 2 and 2. Their sum is2 + 2 = 4. - The absolute difference is
abs(9 - 4) = 5.
Example 2:
Input: nums = [100], k = 1
Output: 0
Explanation:
- The largest element is 100.
- The smallest element is 100.
- The absolute difference is
abs(100 - 100) = 0.
Constraints:
1 <= n == nums.length <= 1001 <= nums[i] <= 1001 <= k <= n
Approach Overview
Problem 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.
Solution
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.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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. |
Video Solution
3774. Absolute Difference Between Maximum and Minimum K Elements (Leetcode Easy) • Programming Live with Larry • 210 views views
Watch 6 more video solutions →Frequently Asked Questions
Is Absolute Difference Between Maximum and Minimum K Elements easy or hard?
Absolute Difference Between Maximum and Minimum K Elements Python/Java solution
How to solve Absolute Difference Between Maximum and Minimum K Elements in O(n)?
What is the best approach for Absolute Difference Between Maximum and Minimum K Elements?
Is Absolute Difference Between Maximum and Minimum K Elements asked at Google/Amazon/Meta?
What data structure is used in Absolute Difference Between Maximum and Minimum K Elements?
What is the time complexity of Absolute Difference Between Maximum and Minimum K Elements?
Ready to solve this problem?
Practice Absolute Difference Between Maximum and Minimum K Elements with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor