beautifulTriplets.js

    0

    0

    The code first creates an array of length d containing the triplet values. It then loops through the array, counting up from 0 to max - 1. For each element in the array, the code calculates the maximum value in the array and assigns that value to the variable result. The code then increments the count variable by 1. Finally, the code returns the result variable.

    function beautifulTriplets(d, arr) {
      let count = arr[0];
      let max = arr[arr.length - 1];
      let result = 0;
    
      let { values } = new Array(arr.length).fill(0).reduce(
        (target, item, index) => {
          target["values"][arr[index]] = target["values"][arr[index]]
            ? (target["values"][arr[index]] += 1)
            : 1;
    
          return target;
        },
        { values: {} }
      );
    
      while (count <= max) {
        values[count] &&
          values[count + d] &&
          values[count + d * 2] &&
          (result += Math.max(
            values[count],
            values[count + d],
            values[count + d * 2]
          ));
    
        count++;
      }
    
      return result;
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.