We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static long countTriplets(List<Long> arr, long r) {
Map<Long, Long> leftDict = new HashMap<>();
Map<Long, Long> rightDict = new HashMap<>();
long tot = 0;
for(int i=0; i<arr.size(); i++){
rightDict.put(arr.get(i), rightDict.getOrDefault(i, 0L)+1);
}
for(int i=0; i<arr.size(); i++){
long curr = arr.get(i);
long left = 0;
long right = 0;
rightDict.put(curr, rightDict.getOrDefault(curr, 0L) - 1);
leftDict.put(curr, leftDict.getOrDefault(curr, 0L) + 1);
if(leftDict.containsKey(curr/r) && curr % r == 0){
left = leftDict.get(curr/r);
}
if(rightDict.containsKey(curr*r)){
right = rightDict.get(curr*r);
}
tot = tot + left * right;
}
return tot;
}
Cookie support is required to access HackerRank
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 →
Whats wrong here guys,