nonDivisibleSubset() takes two arguments: the maximum number of items in the subset and the subset itself. It returns a value of 0 if the subset is not divisible by the maximum number of items.

    The first operation in nonDivisibleSubset() is to create an array of length k + 1 with values initialized to 0. This array will store the results of the calculations performed in thebody of the function.

    The reduce() function is used to calculate the sum of all the integers in the items array that are less than or equal to the given integer k. The calculation is performed in an iterative manner, starting with value at index 0, and working its way up to index k+1. The maximum value of the returned values is then returned.

    Next, the nonDivisibleSubset() function checks to see if k is a multiple of 2. If it is, thefunction adds 1 to the sum of all of the values in the array.

    Finally, thefunction returns the sum of all the values in the array.

    function nonDivisibleSubset(k, s) {
      // Write your code here
      let values = new Array(k).fill(0);
      let result = 0;
      s.reduce((target, item, index) => {
        values[item % k] += 1;
        return target;
      }, []);
      for (let i of Array.from(
        { length: (k + 1) / 2 - 1 },
        (value, index) => index + 1
      )) {
        result += Math.max(values[i], values[k - i]);
      !(k % 2) && !!values[k / 2] && (result += 1);
      values[0] && (result += 1);
      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.