The array partition problem is a common algorithmic challenge where an array is divided into two or more parts based on specific constraints such as equal sum, minimum difference, fixed size, or maximum pairs. Given an integer array nums consisting of 2n integers, your task is to group these integers into n pairs such that:
(a1, b1), (a2, b2), …, (an, bn)
For each pair (ai, bi), take the minimum value from the pair.
Our objective is to maximize the total sum of these minimum values.
Return the maximum possible sum of:

Example 1:
Input: int nums = [1, 4, 3, 2]
Output: 4
Explanation: All possible pairings (ignoring the ordering of elements) are:
(1, 4), (2, 3) → 1 + 2 = 3
(1, 3), (2, 4) → 1 + 2 = 3
(1, 2), (3, 4) → 1 + 3 = 4
The maximum possible sum is 4.
Example 2:
Input: int nums = [6, 2, 6, 5, 1, 2]
Output: 9
Explanation: After optimal pairing: (1, 2), (2, 5), (6, 6).
Sum = 1 + 2 + 6 = 9.
Example 3:
Input: int nums = [7, 3, 1, 0, 0, 6]
Output: 7
Explanation:
Sorted array becomes [0, 0, 1, 3, 6, 7].
Pairs: (0, 0), (1, 3), (6, 7).
Sum = 0 + 1 + 6 = 7.
Example 4:
Input: int nums = [5, 5, 5, 5]
Output: 10
Explanation:
Sorted array = [5, 5, 5, 5].
Pairs: (5, 5), (5, 5).
Sum = 5 + 5 = 10.
Example 5:
Input: int nums = [-1, -2, -3, -4]
Output: -6
Explanation:
Sorted array = [-4, -3, -2, -1].
Pairs: (-4, -3), (-2, -1).
Sum = -4 + (-2) = -6.
A Dual-Pivot Quicksort is used by the Arrays.sort() method to reorder elements in ascending order (for primitives). The smallest element in each pair naturally appears at even indices after sorting. Without explicitly creating pairs, the loop directly accesses these minimal values by increasing by 2. After sorting, these values are effectively combined in linear time using the accumulation variable sum.
Output:
4
Complexity Analysis
The time complexity of the above code is O(N log N) and the space complexity is O(1), where 'N' represents the number of elements in the array.
This method simulates sorting in linear time using a frequency array. Negative numbers are handled via the offset +10000. In order to simulate selecting even-indexed components from a sorted structure, the boolean flag add ensures alternate selection. We use frequency traversal to implicitly recreate sorted order rather than sorting explicitly.
Output:
4
Complexity Analysis
The time complexity of the above code is O(N + k) (where k = 20001) and the space complexity is O(1), where 'N' represents the number of elements in the array.
In Java, a binary heap is used to implement a priority queue. It takes O(log n) for every insertion and delete. We replicate sorted pairing by repeatedly polling the smallest element and deleting the subsequent one. The heap property guarantees efficient minimal retrieval without requiring complete sorting up front.
Output:
4
Complexity Analysis
The time complexity of the above code is O(N log N) and the space complexity is O(N), where 'N' represents the number of elements in the array.
Using Bubble Sort, the array is manually sorted in this method. If adjacent items are out of order, it pushes larger elements to the end by frequently switching them. The argument for choosing an even index is unchanged after sorting. Although it is ineffective for huge inputs, this approach illustrates the principles of algorithms.
Output:
4
Complexity Analysis
The time complexity of the above code is O(N2) (where k = 20001) and the space complexity is O(1), where 'N' represents the number of elements in the array.
The Array Partition Problem is a simple yet powerful example of how greedy strategies and sorting techniques can be used to achieve an optimal solution efficiently. We can maximize the total sum of the pair minimums by sorting the items and choosing the minimum element in each pair.
The significance of comprehending array manipulation, pairing logic, and algorithm optimization methods including sorting, counting sort, and heap structures is illustrated by this problem. Because of its simplicity, effectiveness, and ease of Java implementation, the sorting-based greedy method continues to be the most practical and popular of all the approaches.
Overall, mastering this problem helps strengthen problem-solving skills and builds a strong foundation for tackling more complex array and greedy algorithm problems in coding interviews and real-world applications.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India