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.
Eritorial transformed into Java. Don't forget to check if number is divisible by r.
// Complete the countTriplets function below.staticlongcountTriplets(List<Long>arr,longr){HashMap<Long,Long>right=newHashMap<>();HashMap<Long,Long>left=newHashMap<>();for(Longnum:arr){right.put(num,right.getOrDefault(num,0L)+1);}longtripletsCounter=0;for(Longnum:arr){if(right.getOrDefault(num,0L)>0){right.put(num,right.get(num)-1);}if(num%r==0){tripletsCounter+=right.getOrDefault(num*r,0L)*left.getOrDefault(num/r,0L);}left.put(num,left.getOrDefault(num,0L)+1);}returntripletsCounter;}
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 →
Eritorial transformed into Java. Don't forget to check if number is divisible by r.