Triple sum

  • + 0 comments

    javascript

    function triplets(a, b, c) {
      let cleanA = [...new Set(a)].sort((a, b) => a - b)
      let cleanB = [...new Set(b)].sort((a, b) => a - b)
      let cleanC = [...new Set(c)].sort((a, b) => a - b)
    
      let count = 0
      for (let i = 0; i < cleanB.length; i++) {
        let howManyA = cleanA.findIndex(v => v > cleanB[i])
        let howManyC = cleanC.findIndex(v => v > cleanB[i])
        if (howManyA == -1) howManyA = cleanA.length
        if (howManyC == -1) howManyC = cleanC.length
        
        count += howManyA * howManyC
      }
      return count
    }