The Array Partition solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Array Partition
C++, Python and TypeScript solutions for Array Partition.
Solution files
Python•array-partition/synced-solution.py
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2])
C++•array-partition/synced-solution.cpp
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int total = 0;
for (int i = 0; i < static_cast<int>(nums.size()); i += 2) {
total += nums[i];
}
return total;
}
};
TypeScript•array-partition/synced-solution.ts
function arrayPairSum(nums: number[]): number {
nums.sort((a, b) => a - b);
let total = 0;
for (let i = 0; i < nums.length; i += 2) {
total += nums[i];
}
return total;
}