payal-kothari
7/13/2017 - 4:09 PM

imp : To sort an array, use Arrays.sort(nums) https://leetcode.com/problems/array-partition-i/#/description

imp : To sort an array, use Arrays.sort(nums) https://leetcode.com/problems/array-partition-i/#/description

public class Solution {
    public int arrayPairSum(int[] nums) {
        
        int len = nums.length;
        
        Arrays.sort(nums);              // ********  Sort the Array
        
        int currentNum = 0;
        int sum = nums[currentNum];
        
        while(currentNum < len - 2){ // len is 2n, we are hopping by 2, so till (len -2)
            
            currentNum = currentNum + 2;
            sum = sum + nums[currentNum];
        }
        
        return sum;
    }
}