[JavaScript] 1785. Minimum Elements to Add to Form a Given Sum
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nvar minElements = function (nums, limit, goal) {\n  let sum = nums.reduce((sum, e) => sum + e, 0)\n\n  let cc = 0\n  for (let lim = limit; lim >= 1; lim--) {\n    let need = goal - sum\n    if (need === 0) return cc\n\n    if (lim <= Math.abs(need)) {\n      let q = Math.abs(Math.trunc(need / lim))\n      cc += q\n      sum += q * (need < 0 ? -lim : lim)\n    }\n  }\n  return cc\n};\n\n```
['JavaScript']