Array Partition Problem in Java

Last Updated : 6 Apr 2026

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:

Array Partition Problem in Java

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.

Approach 1: Sorting + Greedy (Optimal Approach)

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.

Example

Compile and Run

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.

Approach 2: Counting Sort Optimization

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.

Example

Compile and Run

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.

Approach 3: Using Priority Queue (Min Heap)

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.

Example

Compile and Run

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.

Approach 4: Manual Sorting (Bubble Sort)

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.

Example

Compile and Run

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.

Advantages of Array Partition

  • Simple Greedy Logic: The Array Partition problem follows a very simple greedy strategy. After sorting the array, selecting the elements at even indices directly gives the optimal solution, making the logic easy to understand and implement.
  • Efficient Implementation: The optimal solution can be implemented using sorting with a time complexity of O(n log n), which is efficient for most practical applications and large datasets.
  • Improves Algorithmic Thinking: This problem helps developers understand important concepts like greedy algorithms, array manipulation, and pairing strategies, which are commonly used in technical interviews and competitive programming.
  • Multiple Solution Approaches: The problem can be solved using different approaches such as sorting, counting sort, priority queue (heap), and brute-force methods. This allows programmers to practice various algorithmic techniques.
  • Handles Negative and Positive Numbers: The algorithm works correctly even when the array contains negative numbers, positive numbers, or a mixture of both.
  • Low Memory Requirement: The most common solution uses in-place sorting and requires only constant extra space, making it memory efficient.

Disadvantages of Array Partition

  • Required an Even-Length Array: The input array is assumed to have 2n elements in this case. The algorithm cannot create full pairs without changing the problem if there are odd numbers of elements.
  • Overhead in Sorting: The array must be sorted in order to find the best solution, which adds an O(n log n) time complexity. This could result in some computing cost for really big datasets.
  • Unsuitable for Data Streaming in Real Time: This method is less appropriate in situations where the data is continuously arriving (streaming data) because it is impractical to sort the complete array each time.
  • Restricted Useful Applications: Compared to other array problems, the problem has less clear real-world applications, despite being a great way to master algorithmic fundamentals.
  • Utilizing Memory in Different Methods: Certain strategies, such as heap-based or counting sort algorithms, could need more RAM, which might not always be the best option given the limitations.
  • Dependence on Constraints: The value range of the items has a significant impact on some optimized methods (like counting sort). These methods become ineffective when the range is quite large.

Conclusion

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.