Skip to main content

Absolute Difference Between Maximum and Minimum K Elements - Solution & Explanation

EasyArraySorting5 min read
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [5,2,2,4], k = 2

Output: 5

Explanation:

  • The k = 2 largest elements are 4 and 5. Their sum is 4 + 5 = 9.
  • The k = 2 smallest elements are 2 and 2. Their sum is 2 + 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 <= 100
  • 1 <= nums[i] <= 100
  • 1 <= 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
SortingO(n log n)O(1) to O(n)Best general solution. Simple implementation using built‑in sort.
Quickselect (Order Statistics)O(n) averageO(1)When only k‑th smallest and k‑th largest values are required.
Heap Based SelectionO(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?
The problem is typically categorized as Easy because the straightforward solution uses sorting and simple indexing. Once the array is sorted, identifying the k-th smallest and k-th largest elements is trivial. The challenge mainly tests understanding of array ordering and indexing.
Absolute Difference Between Maximum and Minimum K Elements Python/Java solution
In Python or Java, sort the array using the built-in sort method. Then read nums[k-1] for the k-th smallest and nums[n-k] for the k-th largest element. Return the absolute difference between them. This implementation runs in O(n log n) time.
How to solve Absolute Difference Between Maximum and Minimum K Elements in O(n)?
Use a selection algorithm such as Quickselect to find order statistics directly. Quickselect can find the k-th smallest element in average O(n) time. Running it for both the k-th smallest and the k-th largest elements avoids sorting the entire array and keeps the overall complexity linear on average.
What is the best approach for Absolute Difference Between Maximum and Minimum K Elements?
The most practical approach is sorting the array. After sorting, the k-th smallest element is at index k-1 and the k-th largest is at index n-k. Compute the absolute difference between these two values. This solution runs in O(n log n) time and requires minimal code.
Is Absolute Difference Between Maximum and Minimum K Elements asked at Google/Amazon/Meta?
Problems involving order statistics, sorting, and array ranking frequently appear in interviews at companies like Amazon, Google, and Meta. Variants often ask for k-th smallest, k-th largest, or differences between ranked elements. Understanding sorting and Quickselect helps solve many of these questions.
What data structure is used in Absolute Difference Between Maximum and Minimum K Elements?
The core data structure is an array. The main algorithmic tool is sorting, though heaps or Quickselect can also be used to find the required order statistics. Heaps are especially useful when tracking the top or bottom k elements efficiently.
What is the time complexity of Absolute Difference Between Maximum and Minimum K Elements?
Using the standard sorting solution, the time complexity is O(n log n) because the array must be sorted first. The final lookup and difference calculation take constant time. Space complexity depends on the sorting implementation and is typically O(1) to O(n).

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 FleetCode