You are viewing a single comment's thread. Return to all comments →
Why does my code fail with R==1. Or any other hints appreciated.
static long countTriplets(long[] arr, long r) { HashMap<Long, Long> map1 = new HashMap(); HashMap<Long, Long> map2 = new HashMap(); long count = 0; for(int i = arr.length - 1; i >= 0; i--) { long a = arr[i]; if(map1.containsKey(a*r)) { long c = map1.get(a*r); map2.put(a, map2.getOrDefault(a, 0L) + c); } if(map2.containsKey(a*r)) count += map2.get(a*r); map1.put(a, map1.getOrDefault(a, 0L) + 1); } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Why does my code fail with R==1. Or any other hints appreciated.