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.
functioncountTriplets(arr,r){constvaluesAfterCurrent=newMap()arr.forEach((v)=>{constcount=valuesAfterCurrent.get(v)?valuesAfterCurrent.get(v)+1:1valuesAfterCurrent.set(v,count)})//all here initiallyconstvaluesBeforeCurrent=newMap()//empty initiallyconstres=arr.reduce((tripletsFound,currentValue)=>{valuesAfterCurrent.set(currentValue,valuesAfterCurrent.get(currentValue)-1)constfrequencyOfFirstsOfTriple=valuesBeforeCurrent.get(currentValue/r)??0constfrequencyOfThirdsOfTriple=valuesAfterCurrent.get(currentValue*r)??0constcount=valuesBeforeCurrent.get(currentValue)?valuesBeforeCurrent.get(currentValue)+1:1valuesBeforeCurrent.set(currentValue,count)returntripletsFound+(frequencyOfFirstsOfTriple*frequencyOfThirdsOfTriple)//if any of the freq is zero it will not increase the current triplets found value},0)returnres}
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 →
Simple Typescript solution with partitioning