You are viewing a single comment's thread. Return to all comments →
Wordy but get the job done.
public static int nonDivisibleSubset(int k, List<Integer> s) { Map<Integer, Integer> modToCount = new HashMap<>(); for (Integer q : s) { int r = q % k; int c = modToCount.getOrDefault(r, 0); c++; modToCount.put(r, c); } if (modToCount.containsKey(0)) { modToCount.put(0, 1); } if (k % 2 == 0 && modToCount.containsKey(k / 2)) { modToCount.put(k / 2, 1); } Set<Integer> skip = new HashSet<>(); int count = 0; for (Integer rem : modToCount.keySet()) { if (skip.contains(rem)) { continue; } int bad = k - rem; int val = modToCount.get(rem); if (modToCount.containsKey(bad)) { if (modToCount.get(bad) > val) { val = modToCount.get(bad); } skip.add(bad); } count += val; } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
Non-Divisible Subset
You are viewing a single comment's thread. Return to all comments →
Wordy but get the job done.