You are viewing a single comment's thread. Return to all comments →
Here's a JavaScript solution based on RobertsN's excellent idea.
const triplets = (arr, r) => { let r2 = {}; let r3 = {}; return arr.reduce((c, v) => { if (r3[v]) { c += r3[v]; } if (r2[v]) { r3[v*r] = (r3[v*r] || 0) + r2[v]; } r2[v*r] = (r2[v*r] || 0) + 1; return c; }, 0); }
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Here's a JavaScript solution based on RobertsN's excellent idea.