561. Array Partition ¶ Time: $O(n) \to O(\texttt{sort})$ Space: $O(1) \to O(\texttt{sort})$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public: int arrayPairSum(vector<int>& nums) { int ans = 0; ranges::sort(nums); for (int i = 0; i < nums.size(); i += 2) ans += nums[i]; return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12class Solution { public int arrayPairSum(int[] nums) { int ans = 0; Arrays.sort(nums); for (int i = 0; i < nums.length; i += 2) ans += nums[i]; return ans; } } 1 2 3class Solution: def arrayPairSum(self, nums: list[int]) -> int: return sum(sorted(nums)[::2])