In competitive programming, I was solving a given problem - given an array nums of non-negative integers, and a target sum S, we have to find out the number of ways we can obtain target sum from sum of given numbers (where each nums[i] can be taken as nums[i] or -nums[i].
Although I came across some solutions that mainly relied on direct access tables using array (it is given that sum of numbers cannot exceed 1000), but I tried it using HashMap to reduce the space required. My code is as follows -
public int findTargetSumWays(int[] nums, int S) {
Map<Integer, Integer> dp = new HashMap();
dp.put(nums[0], 1);
dp.put(-nums[0], dp.getOrDefault(-nums[0], 0) + 1);
for (int i=1; i<nums.length; i++) {
for (Integer sum : dp.keySet()) {
dp.put(sum+nums[i], dp.getOrDefault(sum+nums[i], 0) + 1);
dp.put(sum-nums[i], dp.getOrDefault(sum-nums[i], 0) + 1);
}
}
return dp.get(S);
}
But I am getting ConcurrentModificationException on running the code. I tried finding about the issue. Although I got some conceptual understanding that in iteration, view of Collections can't be structurally modified, I am not able to figure out how to find my way around it to find a solution.
Is it that a solution using HashMap(or any dynamic data structure) is not possible? Any help is appreciated.