Java solution with explanation


  • 0
    L

    This is O(nlogn) Java solution that will use sorting as a basis. The idea is to sort and pick every pair and we deal with the minimum only.

    public int arrayPairSum(int[] nums) {
            if(nums.length==0) {
                return 0;
            }
            int sum=0;
            Arrays.sort(nums);
            for(int i=1;i<nums.length;i=i+2) {
                sum += nums[i-1];
            }
            return sum;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.