I am trying to solve the problem it works for positive integer arrays but fails for negative test cases. How to handle the below test case?
You are given an integer array nums of 2 * n integers. You need to partition nums into two arrays of length n to minimize the absolute difference of the sums of the arrays. To partition nums, put each element of nums into one of the two arrays. Return the minimum possible absolute difference.
I am able to get the possible sum for these negative numbers and store in the list of the map but, when I iterate through the map values to find the absolute difference I cannot get the correct value as 0 instead I am getting the value as 6 because in the memorization map possible sum as -3 added it to the target we get 3. so the absolute difference becomes 6
How to handle this scenario? Refer to the below comment in the code.
Code that needs to re-visited for negative integers scenario
Test case: Input: nums = [2,-1,0,4,-2,-9] Output: 0 Explanation: One optimal partition is: [2,4,-9] and [-1,0,-2]. The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0.
package com.dynamicProgramming;
import java.util.*;
public class PartitionSum {
List<HashMap<Integer,Boolean>> ll = new ArrayList<>();
public int minimumDifference(int[] nums) {
int n = nums.length;
int range = 0;
for(int i =0;i<nums.length;i++){
ll.add(new HashMap<>());
range+=nums[i];
}
// System.out.println(range/2);
findSub(nums ,n-1,range);
int min=Integer.MAX_VALUE;
//Code that needs to re-vistied for negative integers scenario
for(HashMap<Integer,Boolean> h:ll){
for(Map.Entry<Integer,Boolean> map:h.entrySet()){
if(map.getValue()){
int s2= (range<0)?range+map.getKey():range-map.getKey();
System.out.println(map.getKey()+ " "+s2);
int diff=Math.abs(s2-map.getKey());
min=Math.min(min,diff);
}
}
}
return min;
}
private boolean findSub(int[] nums, int i, int sum) {
if(sum==0)
return true;
if(i==0){
return nums[i]==sum;
}
if(ll.get(i).containsKey(sum)) {
return ll.get(i).get(sum);
}
boolean p=false;
if(sum>=nums[i])
p=findSub(nums,i-1,sum-nums[i]);
boolean np=findSub(nums,i-1,sum);
ll.get(i).put(sum,p||np);
return p||np;
}
public static void main(String[] args) {
PartitionSum partitionSum= new PartitionSum();
int[] nums={2,-1,0,4,-2,-9};
System.out.println(partitionSum.minimumDifference(nums));
System.out.println(partitionSum.ll);
}
}
n
?