1-liner Solutions (C++, Python, JS) | beats 100% | short & easy w/ explanation
Solution -\nThe solution can be arrived at using a greedy approach. We just need to find the current sum, get the absolute differene of the current sum and goal
**Solution -**\nThe solution can be arrived at using a greedy approach. We just need to find the current sum, get the absolute differene of the current sum and `goal`, and keep adding `limit` (since that\'s the biggest element we can add so as to get smallest number of additions). The number of times we need to add `limit` can be determined by dividing the absolute difference of `goal` and `sum` with limit and taking its ceil value.\n\n----------\n**C++**\n```\nint minElements(vector<int>& nums, int limit, int goal) {\n        return ceil( abs( (goal*1.0) - accumulate(begin(nums), end(nums), 0L)) / limit);\n}\n```\n\n-------------\n**Python**\n```\ndef minElements(self, nums: List[int], limit: int, goal: int) -> int:\n\treturn ceil( abs(goal - sum(nums) ) / limit)\n```\n\n----------\n\n**JavaScript**\n\n```\nvar minElements = function(nums, limit, goal) {\n    return Math.ceil(Math.abs(goal - nums.reduce((a, b) => a + b, 0)) / limit);\n};\n```\n\n--------------\n\n**Time Complexity** : **`O(N)`**, for iterating over `nums` once.\n**Space Complexity** : **`O(1)`**, since only constant space is used.\n\n\n\n\n
[]