You are viewing a single comment's thread. Return to all comments →
You could achieve this with .reduce
function countTriplets(arr, r) { let count = 0; arr.reduce((acc, val) => { if (!acc[val]) { acc[val] = { s1: 0, s2: 0, s3: 0 }; } if (!acc[val * r]) { acc[val * r] = { s1: 0, s2: 0, s3: 0 }; } count += acc[val].s3; acc[val * r].s3 += acc[val].s2; acc[val * r].s2 += 1; return acc; }, {}); return count; }
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 →
You could achieve this with .reduce