Triple sum

  • + 0 comments

    JS all pass

    function triplets(a, b, c) {
      let cleanA = [...new Set(a)].sort((x, y) => x - y)
      let cleanB = [...new Set(b)]
      let cleanC = [...new Set(c)].sort((x, y) => x - y)
      let count = 0
    
      for (let i = 0; i < cleanB.length; i++) {
        let numOfP = 0, numOfQ = 0
        for (let j = cleanA.length; j > 0; j--) {
          if (cleanA[j - 1] <= cleanB[i]) {
            numOfP = j
            break
          }
        }
        for (let j = cleanC.length; j > 0; j--) {
          if (cleanC[j - 1] <= cleanB[i]) {
            numOfQ = j
            break
          }
        }
        count += numOfP * numOfQ
      }
      return count
    }