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.
Thank you for this solution. Got it after a long time. But somehow still failing for corner case r==1. And tracing through, I dont understand how tht is covered here. So wrote an addition check for r==1:
int len=arr.size();
Map<Long, Long> map2 = new HashMap<>();
Map<Long, Long> map3 = new HashMap<>();
Long val=0L;
long count=0;
if (r==1) {
for (int i=0; i<len; i++) {
val = arr.get(i);
map2.put(val, map2.getOrDefault(val,0L)+1);
}
for (Long values: map2.values()){
count+=values*(values-1)*(values-2)/6; //nCr
}
return count;
}
for (int i=0; i<len; i++) {
val = arr.get(i);
map2.put(val*r, map2.getOrDefault(val*r, 0L)+1);
if(map2.get(val)!=null) {
map3.put(val*r, map3.getOrDefault(val*r, 0L)+map2.get(val));
}
count+=map3.getOrDefault(val, 0L);
}
return count;
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 →
Thank you for this solution. Got it after a long time. But somehow still failing for corner case r==1. And tracing through, I dont understand how tht is covered here. So wrote an addition check for r==1: