You are viewing a single comment's thread. Return to all comments →
Your solution is very compact, although an alternative is to use Map.getDefaultOr so you don't have to use many null comparions.
static long countTriplets(List<Long> arr, long r) { Map<Long, Long> t2 = new HashMap<>(); Map<Long, Long> t3 = new HashMap<>(); long result = 0L; for(Long a : arr) { result += t3.getOrDefault(a, 0L); if (t2.containsKey(a)){ t3.put(a*r, t3.getOrDefault(a*r, 0L) + t2.get(a)); } t2.put(a*r, t2.getOrDefault(a*r, 0L) + 1); } return result; }
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 →
Your solution is very compact, although an alternative is to use Map.getDefaultOr so you don't have to use many null comparions.