#561
Easy Algorithms Array partition
Array Greedy Sorting Counting Sort
81.5% acceptance
Jan 13, 2026
2394
302
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn array_pair_sum(nums: Vec<i32>) -> i32 {
let mut sorted = nums;
sorted.sort_unstable();
sorted.iter().step_by(2).sum()
}
}