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.
I'm learning Java, this solution was converted from @Jugad's Javascript solution, but I'm failing test cases 6 and 10. Can anyone help?
// Complete the countTriplets function below.staticlongcountTriplets(List<Long>arr,longr){//0. InitializationsMap<Long,Long>singleCounts=newHashMap<>();Map<Long,Long>pairCounts=newHashMap<>();longtriplets=0;//1. Sort array ascending orderCollections.sort(arr,Collections.reverseOrder());//2. Iterate through sorted arrayfor(Longnum:arr){// A. Count completed pairsif(pairCounts.get(num*r)!=null){triplets+=pairCounts.get(num*r);}// B. Increment pair counts if neededif(singleCounts.get(num*r)!=null){pairCounts.merge(num,singleCounts.get(num*r),Long::sum);}// C. Increment single countssingleCounts.merge(num,Long.valueOf(1),Long::sum);}returntriplets;}
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Hello!
I'm learning Java, this solution was converted from @Jugad's Javascript solution, but I'm failing test cases 6 and 10. Can anyone help?