You are viewing a single comment's thread. Return to all comments →
That's great. My Java version is here:
public class DivisibleSumPairs { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); Map<Integer, Integer> buckets = new HashMap<>(); int pairs = 0; for(int a_i=0; a_i < n; a_i++){ int num = in.nextInt(); num %= k; int complement = (k-num) % k; Integer count = buckets.get(complement); if (count != null) { pairs += count; } count = buckets.get(num); if (count == null) { buckets.put(num, 1); } else { buckets.put(num, count+1); } } System.out.println(pairs); } }
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
That's great. My Java version is here: