Minimum Elements to Add to Form a Given Sum LeetCode solution in C++
1class Solution {2public:3int minElements(vector<int> &nums, int limit, int goal) {4// just calculate how many limit values to be added to the array to make our5// currSum close to the goal6long int currSum = 0;7for (auto ele : nums) currSum += ele;8long int remaining = abs(currSum - goal);9if (remaining == 0) return 0;10long int res = (remaining / limit);11// if remaining exists still after adding limit k times12// ex - remaining - 7 limit -3 we have to add a 1 after adding two times of13// limit values (6)14res += (remaining % limit != 0) ? 1 : 0;15return res;16}17};