question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
minimum-elements-to-add-to-form-a-given-sum
[Java/Python 3] 2/1 liners.
javapython-3-21-liners-by-rock-1k6b
java\n public int minElements(int[] nums, int limit, int goal) {\n long diff = goal - IntStream.of(nums).mapToLong(i -> i).sum();\n return (int
rock
NORMAL
2021-03-07T04:02:52.537603+00:00
2021-03-07T04:02:52.537655+00:00
613
false
```java\n public int minElements(int[] nums, int limit, int goal) {\n long diff = goal - IntStream.of(nums).mapToLong(i -> i).sum();\n return (int)((Math.abs(diff) + limit - 1) / limit); \n }\n```\n```python\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return (abs(goal - sum(nums)) + limit - 1) // limit\n```
9
4
[]
0
minimum-elements-to-add-to-form-a-given-sum
Python 3 | 1-liner | Explanation
python-3-1-liner-explanation-by-idontkno-8ql7
Explanation\n- Find the absolute difference\n- Add new numbers using limit, count how many numbers need to be added using ceil\n### Implementation\n\nclass Solu
idontknoooo
NORMAL
2021-03-21T20:19:07.019598+00:00
2021-03-21T20:19:07.019631+00:00
895
false
### Explanation\n- Find the absolute difference\n- Add new numbers using `limit`, count how many numbers need to be added using `ceil`\n### Implementation\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return math.ceil(abs(goal - sum(nums)) / limit)\n```
6
0
['Greedy', 'Python', 'Python3']
1
minimum-elements-to-add-to-form-a-given-sum
Python 1-line solution using ceil
python-1-line-solution-using-ceil-by-oto-qd5t
\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return math.ceil( abs(goal - sum(nums)) / limit) \n
otoc
NORMAL
2021-03-07T04:09:45.225510+00:00
2021-03-07T04:09:45.225550+00:00
470
false
```\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return math.ceil( abs(goal - sum(nums)) / limit) \n```
6
0
[]
1
minimum-elements-to-add-to-form-a-given-sum
Java Greedy Solution
java-greedy-solution-by-admin007-kocl
\npublic int minElements(int[] nums, int limit, int goal) {\n\tlong sum = 0;\n\tfor (int i = 0; i < nums.length; i++) {\n\t\tsum += nums[i];\n\t}\n\tlong delta
admin007
NORMAL
2021-03-12T19:21:30.967885+00:00
2021-03-12T19:23:41.453658+00:00
646
false
```\npublic int minElements(int[] nums, int limit, int goal) {\n\tlong sum = 0;\n\tfor (int i = 0; i < nums.length; i++) {\n\t\tsum += nums[i];\n\t}\n\tlong delta = (long)goal - sum;\n\treturn (int)((Math.abs(delta) + limit - 1) / limit); // You will need the ceil in such a integer division here.\n}\n```
5
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
Python math ceil
python-math-ceil-by-it_bilim-wo24
\n\ndef minElements(self, nums, limit, goal):\n import math\n t = float(sum(nums)) # count sum of all elements\n s = abs(goal
it_bilim
NORMAL
2021-03-07T04:01:26.948814+00:00
2021-03-07T04:17:42.234185+00:00
627
false
```\n\ndef minElements(self, nums, limit, goal):\n import math\n t = float(sum(nums)) # count sum of all elements\n s = abs(goal -t) # find diffence between goal and sum\n return int(math.ceil(s/limit)) # find how many numbers can be added (nums[i]<=limit)\n \n \n```
5
3
['Python']
1
minimum-elements-to-add-to-form-a-given-sum
Easy C++ solution || commented
easy-c-solution-commented-by-saiteja_bal-23a1
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n \n //just calculate how many limit values to be added to
saiteja_balla0413
NORMAL
2021-06-18T06:26:53.841950+00:00
2021-06-18T06:26:53.841993+00:00
576
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n \n //just calculate how many limit values to be added to the array to make our currSum close to the goal\n long int currSum=0;\n for(auto ele:nums)\n currSum+=ele;\n long int remaining=abs(currSum-goal);\n if(remaining==0)\n return 0;\n long int res=(remaining/limit);\n //if remaining exists still after adding limit k times\n //ex - remaining - 7 limit -3 we have to add a 1 after adding two times of limit values (6)\n res+= (remaining%limit!=0) ? 1 : 0;\n return res;\n \n }\n};\n```\n**Do upvote if this helps you :)**
4
0
['Greedy', 'C']
0
minimum-elements-to-add-to-form-a-given-sum
Simple Java Solution
simple-java-solution-by-kodiswaran-02dd
\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for(int num: nums)\n sum += num;\n
kodiswaran
NORMAL
2021-03-07T04:00:50.722330+00:00
2021-03-07T04:00:50.722374+00:00
608
false
```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for(int num: nums)\n sum += num;\n long diff = Math.abs(sum-goal);\n return (int) (diff/limit) + (diff%limit>0?1:0);\n }\n}\n```
4
1
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
C++ | Easy to Understand Solution O(n)
c-easy-to-understand-solution-on-by-user-y36s
class Solution {\npublic:\n int minElements(vector& nums, int limit, int goal) {\n long long sum = 0;\n for(int i=0;i<nums.size();i++){\n
user2921bc
NORMAL
2022-05-10T11:40:30.545932+00:00
2022-05-10T11:40:30.545961+00:00
445
false
class Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int i=0;i<nums.size();i++){\n sum += nums[i];\n }\n if(sum == goal){\n return 0;\n }\n long long diff = abs(goal-sum);\n long long ans = diff/limit;\n if(diff%limit==0){\n return ans;\n }\n return ans+1;\n }\n};
3
0
['C']
0
minimum-elements-to-add-to-form-a-given-sum
Fastest and Simplest greedy solution - Beats 100% time and 100% memory - With detailed explanation
fastest-and-simplest-greedy-solution-bea-m5o7
We start by calculating the total sum of the elements of the array.\n * We then simplify our problem by making the new goal as abs(goal-sum) and starting sum
yajassardana
NORMAL
2021-03-07T04:36:17.533578+00:00
2021-03-07T05:31:27.194478+00:00
164
false
* We start by calculating the **total sum** of the elements of the array.\n * We then simplify our problem by making the new goal as **abs(goal-sum)** **and starting sum as 0**.\n * Then comes the** most important part** --> We\'ve to calculate count by counting the number of times a limit value will come while adding upto the goal. \n * **To prevent TLE** --> We calculate this by **dividing the goal with the limit**. In case a remainder is left, as t**he remainder will be smaller than the limit**, we simply return the** calculated count + 1** for the remainder.\n```\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int n:nums){\n sum+=n;\n }\n int count = 0;\n long long tgoal = abs(goal - sum);\n int min = tgoal/limit;\n if(tgoal%limit==0)return min;\n else return min+1;\n }\n```
3
1
['Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
1-liner Solutions (C++, Python, JS) | beats 100% | short & easy w/ explanation
1-liner-solutions-c-python-js-beats-100-hhbip
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
archit91
NORMAL
2021-03-07T04:08:25.970789+00:00
2021-03-07T04:33:43.653006+00:00
100
false
**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
3
2
[]
0
minimum-elements-to-add-to-form-a-given-sum
Clean Python 3, straightforward
clean-python-3-straightforward-by-lenche-a20t
Time: O(N + diff / limit)\nSpace: O(1)\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n total = sum(nums)\
lenchen1112
NORMAL
2021-03-07T04:03:20.766943+00:00
2021-03-07T04:03:20.766986+00:00
192
false
Time: `O(N + diff / limit)`\nSpace: `O(1)`\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n total = sum(nums)\n diff = abs(total - goal)\n return diff // limit + (diff % limit > 0)\n```
3
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
[Python3] 1-line
python3-1-line-by-ye15-ce2g
\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal - sum(nums))/limit)\n
ye15
NORMAL
2021-03-07T04:01:19.795339+00:00
2021-03-07T04:01:19.795383+00:00
319
false
\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal - sum(nums))/limit)\n```
3
1
['Python3']
1
minimum-elements-to-add-to-form-a-given-sum
Python Elegant & Short | 1-Line
python-elegant-short-1-line-by-kyrylo-kt-hoay
Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n\nfrom math import ceil\n\nclass Solution:\n def minElements(self, nums: List[int],
Kyrylo-Ktl
NORMAL
2023-03-19T22:01:58.013017+00:00
2023-03-19T22:01:58.013055+00:00
470
false
# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nfrom math import ceil\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(sum(nums) - goal) / limit)\n\n```
2
0
['Python3']
1
minimum-elements-to-add-to-form-a-given-sum
C++ Easy to understand O(n) greedy solution
c-easy-to-understand-on-greedy-solution-cxef5
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nFirstly we will calcula
Mdkaif2938
NORMAL
2023-02-19T15:39:39.223219+00:00
2023-02-19T15:39:39.223263+00:00
359
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirstly we will calculate the total sum of array\nThen find how far are we from the goal\nthen calculate the number of elements needed to reach the GOAL.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n \n long long int sum = 0, diff;\n\n //finding total sum of array\n for(int x = 0; x < nums.size(); x++)sum+=nums[x];\n\n //calculating absolute difference of goal and sum\n diff = (goal>=sum) ? goal-sum : sum-goal;\n\n //calculate number of elements(<=limit) to fulfill the difference \n return (diff%limit) ? diff/limit+1 : diff/limit;\n }\n};\n```
2
0
['Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
Easy to understand Greedy solution| Javascript | tc: O(n) | sc: O(1)
easy-to-understand-greedy-solution-javas-hrho
Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co
aaryansinha16
NORMAL
2023-01-26T19:26:22.975051+00:00
2023-01-26T19:26:22.975089+00:00
134
false
# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @param {number} limit\n * @param {number} goal\n * @return {number}\n */\nvar minElements = function(nums, limit, goal) {\n var sum = 0\n for(var i = 0; i<nums.length; i++) sum += nums[i]\n if(sum === goal) return 0\n var diff = Math.abs(goal - sum)\n\n if(diff <= limit) return 1\n\n if(diff % limit == 0) return diff/limit\n var ans = Math.floor(diff/limit) + 1\n return ans\n\n};\n```
2
0
['Greedy', 'JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
Java || Greedy || 1ms || beats 100% in time and memory || T.C - O(N) S.C - O(1)
java-greedy-1ms-beats-100-in-time-and-me-klsw
\n public int minElements(int[] nums, int limit, int goal) {\n \n int count = 0,len = nums.length;\n long sum = 0;\n for(int i=0;
LegendaryCoder
NORMAL
2021-03-07T09:01:45.537700+00:00
2021-03-07T09:01:45.537746+00:00
71
false
\n public int minElements(int[] nums, int limit, int goal) {\n \n int count = 0,len = nums.length;\n long sum = 0;\n for(int i=0;i<len;i++)\n sum+=nums[i];\n \n if(sum == goal)\n return 0;\n \n long diff = Math.abs(goal - sum);\n \n int rem = (int) (diff%limit);\n int quot = (int) (diff/limit);\n \n if(rem == 0)\n return quot;\n \n return quot+1;\n }\n
2
1
[]
0
minimum-elements-to-add-to-form-a-given-sum
Java || One Pass
java-one-pass-by-vishnu_jupiter-daro
\tclass Solution {\n \n\t\tpublic int minElements(int[] nums, int limit, int goal) {\n \n\t\t\tlong sum = 0, temp;\n\n\t\t\tfor(int i : nums)\n\t\t\t\
Vishnu_Jupiter
NORMAL
2021-03-07T05:38:55.354296+00:00
2021-03-07T05:38:55.354343+00:00
187
false
\tclass Solution {\n \n\t\tpublic int minElements(int[] nums, int limit, int goal) {\n \n\t\t\tlong sum = 0, temp;\n\n\t\t\tfor(int i : nums)\n\t\t\t\tsum += i;\n\t\t\tif(sum == goal) // if sum and goal is equal then no element is required \n\t\t\t\treturn 0;\n\t\t\ttemp = Math.abs(sum - goal);\n\t\t\treturn (int)((temp / limit) + ((temp % limit) > 0 ? 1 : 0)); // calculate the elements needed\n\t\t}\n\t}
2
1
['Math', 'Java']
0
minimum-elements-to-add-to-form-a-given-sum
C++ Short Greedy Solution
c-short-greedy-solution-by-niloiv-mre5
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long sum = 0;\n for (int num : nums) {\n s
niloiv
NORMAL
2021-03-07T04:27:14.349488+00:00
2021-03-07T04:27:14.349517+00:00
340
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long sum = 0;\n for (int num : nums) {\n sum += num;\n }\n long target = goal - sum;\n return target % limit == 0 ? abs(target / limit) : abs(target/limit) + 1;\n }\n};\n```
2
0
[]
1
minimum-elements-to-add-to-form-a-given-sum
c++ easy to understand clean code
c-easy-to-understand-clean-code-by-codin-71ln
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long sum = 0;\n for (auto& num : nums) sum += num;\n
codingmission
NORMAL
2021-03-07T04:03:03.568513+00:00
2021-03-07T04:04:58.390066+00:00
169
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long sum = 0;\n for (auto& num : nums) sum += num;\n long diff = abs(goal - sum);\n return diff % limit ? (diff / limit) + 1 : diff / limit;\n }\n};\n```
2
1
['C']
0
minimum-elements-to-add-to-form-a-given-sum
Simple C++ solution
simple-c-solution-by-gcsingh1629-ccz6
Code\n\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=0;\n for(auto n:nums){\n
gcsingh1629
NORMAL
2023-10-15T05:26:37.714340+00:00
2023-10-15T05:26:37.714365+00:00
90
false
# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=0;\n for(auto n:nums){\n sum+=n;\n }\n long long diff=abs(goal-sum);\n return diff%limit==0?diff/limit:(int)(diff/limit+1);\n }\n};\n```
1
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Only 2 Lines Code || Easy C++ ✅✅
only-2-lines-code-easy-c-by-deepak_5910-rzxk
Approach\n Describe your approach to solving the problem. \nResult = abs(Current_Sum - Goal) / Limit + (abs(Current_sum-Goal)%Limit!=0)\n# Complexity\n- Time co
Deepak_5910
NORMAL
2023-08-11T18:55:40.439895+00:00
2023-08-11T18:55:40.439949+00:00
233
false
# Approach\n<!-- Describe your approach to solving the problem. -->\n`Result = abs(Current_Sum - Goal) / Limit + (abs(Current_sum-Goal)%Limit!=0)`\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& arr, int limit, int g) {\n long long sum = accumulate(arr.begin(),arr.end(),1LL*0);\n return 1LL*abs(g - sum)/limit+(abs(g-sum)%limit>=1);\n }\n};\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/0867d213-2b71-4436-9615-8ea807874958_1691780034.1849213.jpeg)\n
1
0
['Greedy', 'C++']
1
minimum-elements-to-add-to-form-a-given-sum
[JavaScript] 1785. Minimum Elements to Add to Form a Given Sum
javascript-1785-minimum-elements-to-add-q9mhm
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
pgmreddy
NORMAL
2023-08-06T14:30:43.471883+00:00
2023-08-06T14:30:43.471914+00:00
150
false
# 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```
1
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
Short & Concise | C++
short-concise-c-by-tusharbhart-v02x
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int i : nums) sum += i;\n
TusharBhart
NORMAL
2023-06-15T18:38:28.926211+00:00
2023-06-15T18:38:28.926233+00:00
227
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int i : nums) sum += i;\n \n long long diff = goal - sum;\n return ceil((double)abs(diff) / limit);\n }\n};\n```
1
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Python 3 solution - O(1) solution
python-3-solution-o1-solution-by-ankisha-jb56
Intuition and Approach\n\nTo solve this problem, We need to find the minimum number of elements that we can add to the array to reach the goal sum while maintai
ankits0052
NORMAL
2023-04-19T20:22:15.360547+00:00
2023-04-19T20:22:15.360589+00:00
92
false
# Intuition and Approach\n\nTo solve this problem, We need to find the minimum number of elements that we can add to the array to reach the goal sum while maintaining the property that abs(nums[i]) <= limit for all i.\n\nFirst, we calculate the current sum of the array. Then, we calculate the difference between the goal and the current sum. Since we want to add the minimum number of elements, we should try to add elements with the maximum possible absolute value, which is \'limit\'. We can then calculate how many times we need to add \'limit\' to reach the goal sum.\n\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n current_sum = sum(nums)\n \n # Calculate the difference between the goal sum and the current sum\n diff = abs(goal - current_sum)\n \n # Calculate the minimum number of elements required to reach the goal sum\n min_elements = (diff + limit - 1) // limit\n \n return min_elements\n```
1
0
['Python3']
1
minimum-elements-to-add-to-form-a-given-sum
JAVA one line solution
java-one-line-solution-by-21arka2002-w5vj
\n\n# Code\n\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n return (int)(Math.ceil((Math.abs((long)goal-(Arrays.strea
21Arka2002
NORMAL
2023-04-03T10:31:12.561960+00:00
2023-04-03T10:34:20.591232+00:00
586
false
\n\n# Code\n```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n return (int)(Math.ceil((Math.abs((long)goal-(Arrays.stream(Arrays.stream(nums).mapToLong(i->i).toArray()).sum())))*1.0/limit));\n }\n}\n```\n\n```\n//Convert integer array to long array\nlong[] longArray = Arrays.stream(nums).mapToLong(i -> i).toArray();\n\n//Sum of elements in array integer/long\nlong s= Arrays.stream(longArray).sum();\n```\n\n
1
0
['Array', 'Math', 'Greedy', 'Java']
0
minimum-elements-to-add-to-form-a-given-sum
C++ | greedy | fast
c-greedy-fast-by-venomhighs7-avaq
\n\n# Code\n\nclass Solution {\npublic:\n int minElements(vector<int>& A, int limit, int goal) {\n long sum = accumulate(A.begin(), A.end(), 0L),
venomhighs7
NORMAL
2022-11-15T03:59:38.975987+00:00
2022-11-15T03:59:38.976012+00:00
811
false
\n\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& A, int limit, int goal) {\n long sum = accumulate(A.begin(), A.end(), 0L), diff = abs(goal - sum);\n return (diff + limit - 1) / limit;\n }\n};\n```
1
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
c++ using greedy
c-using-greedy-by-yashodhan11-opog
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long total=0;\n for(int i=0;i<nums.size();i++){\
Yashodhan11
NORMAL
2022-10-03T13:31:55.712497+00:00
2022-10-03T13:31:55.712535+00:00
345
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long total=0;\n for(int i=0;i<nums.size();i++){\n total+=nums[i];\n }\n long long count=0;\n long long target=abs(goal-total);\n while(target && limit){\n count+=target/limit;\n target-=((target)/limit)*limit;\n \n limit--;\n }\n return count;\n }\n};\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
Python. One liner. Faster than 69%...
python-one-liner-faster-than-69-by-nag00-8zin
\nclass Solution:\n def minElements(self, x: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal-sum(x))/limit)\n
nag007
NORMAL
2022-06-24T17:16:58.512352+00:00
2022-06-24T17:17:14.893801+00:00
183
false
```\nclass Solution:\n def minElements(self, x: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal-sum(x))/limit)\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
[Py/Py3] Solution w/ comments
pypy3-solution-w-comments-by-ssshukla26-itya
\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n """\n 1) Take the sum of nums\n 2) Chech how
ssshukla26
NORMAL
2021-09-12T18:27:48.618018+00:00
2021-09-12T18:27:48.618066+00:00
201
false
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n """\n 1) Take the sum of nums\n 2) Chech how many steps it is aways from goal\n 3) Divide the steps w.r.t limit\n 4) The ceiling of the division will give you\n no. of hopes required to reach that goal\n """\n return math.ceil(abs(goal - sum(nums))/limit)\n```
1
0
['Greedy', 'Python', 'Python3']
0
minimum-elements-to-add-to-form-a-given-sum
One liners, 98% speed
one-liners-98-speed-by-evgenysh-tqrs
Runtime: 708 ms, faster than 98.60%\nMemory Usage: 26.8 MB, less than 96.28%\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: in
evgenysh
NORMAL
2021-07-16T16:04:34.119839+00:00
2021-07-16T16:04:34.119969+00:00
233
false
Runtime: 708 ms, faster than 98.60%\nMemory Usage: 26.8 MB, less than 96.28%\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal - sum(nums)) / limit)\n```
1
0
['Python', 'Python3']
0
minimum-elements-to-add-to-form-a-given-sum
C++ Easy Short Math Solution
c-easy-short-math-solution-by-vibhu17200-3ai9
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=0;\n for(int i=0;i<nums.size();i++)\n
vibhu172000
NORMAL
2021-07-04T02:25:22.713697+00:00
2021-07-04T02:25:22.713727+00:00
112
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=0;\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n }\n int ans=0;\n long long x = goal-sum;\n x=abs(x);\n if(x%limit==0)\n {\n ans=x/limit;\n return ans;\n }\n ans=x/limit;\n ans+=1;\n return ans;\n }\n};\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
It should have been classified as Easy
it-should-have-been-classified-as-easy-b-421y
\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar splitString = function(s) {\n const len = s.length\n \n function toKey(fromIndex, fromVal)
lilongxue
NORMAL
2021-05-02T15:47:03.265653+00:00
2021-05-02T15:47:03.265690+00:00
69
false
```\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar splitString = function(s) {\n const len = s.length\n \n function toKey(fromIndex, fromVal) {\n return [fromIndex, fromVal].join(\'|\')\n }\n // key => result of key\n const map = new Map()\n function isOK(fromIndex, fromVal) {\n if (fromIndex === len) return true\n \n const key = toKey(fromIndex, fromVal)\n if (map.has(key)) return map.get(key)\n \n const seekMe = -1 + fromVal\n let result = false\n for (let endIndex = fromIndex; endIndex < len; endIndex++) {\n const val = Number(s.slice(fromIndex, 1 + endIndex))\n if (val === seekMe) {\n if (seekMe !== 0) {\n result = isOK(1 + endIndex, seekMe)\n break\n } else {\n result = result || isOK(1 + endIndex, seekMe)\n }\n }\n }\n map.set(key, result)\n \n return result\n }\n \n \n for (let endIndex = 0; endIndex < -1 + len; endIndex++) {\n const fromIndex = 1 + endIndex\n const fromVal = Number(s.slice(0, 1 + endIndex))\n let subresult = isOK(fromIndex, fromVal)\n if (subresult) return subresult\n }\n \n \n return false\n};\n```
1
1
[]
0
minimum-elements-to-add-to-form-a-given-sum
[JavaScript] Easy to understand - 1-line greedy
javascript-easy-to-understand-1-line-gre-was6
For this problem, the key point is that we could add multiple identical numbers, which means it\'s straightforward to solve it via greedy strategy.\n\njs\nconst
poppinlp
NORMAL
2021-03-22T09:47:58.181382+00:00
2021-03-22T09:51:55.521890+00:00
174
false
For this problem, the key point is that we could add multiple identical numbers, which means it\'s straightforward to solve it via greedy strategy.\n\n```js\nconst minElements = (nums, limit, goal) => {\n let diff = goal;\n for (let i = 0; i < nums.length; ++i) {\n diff -= nums[i];\n }\n return Math.ceil(Math.abs(diff) / limit);\n};\n```\n\nAnd, sure, we can make it 1-line easily:\n\n```js\nconst minElements = (nums, limit, goal) => Math.ceil(Math.abs(nums.reduce((prev, cur) => prev - cur, goal) / limit));\n```
1
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
C++ solution
c-solution-by-oleksam-iutn
\nint minElements(vector<int>& nums, int limit, int goal) {\n\tlong sum = accumulate(nums.begin(), nums.end(), 0L);\n\treturn ceil((double)abs(goal - sum) / lim
oleksam
NORMAL
2021-03-21T19:13:31.901579+00:00
2021-03-21T19:32:13.514022+00:00
224
false
```\nint minElements(vector<int>& nums, int limit, int goal) {\n\tlong sum = accumulate(nums.begin(), nums.end(), 0L);\n\treturn ceil((double)abs(goal - sum) / limit);\n}\n```\n
1
0
['C', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
Python Concise solution 100% Faster and 100% less memory usage (3-liner)
python-concise-solution-100-faster-and-1-4gim
First of all, we need to find the absolute difference in the sum of the array and the goal to find the total sum of the values that are to be added in it. Then
tier10coder
NORMAL
2021-03-19T10:42:28.417013+00:00
2021-03-20T03:14:06.072603+00:00
160
false
First of all, we need to find the absolute difference in the sum of the array and the goal to find the total sum of the values that are to be added in it. Then we simply find the ceiling value of the absolute difference divided by the limit. Using the if else condtion instead of math.floor makes the code faster .\n\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n x=sum(nums)\n x=abs(x-goal)\n return x//limit + (1 if x%limit!=0 else 0)\n
1
0
['Greedy', 'Python']
0
minimum-elements-to-add-to-form-a-given-sum
Java 3 lines: meaningless question. Better to modify as...
java-3-lines-meaningless-question-better-wjek
The question is too simple as medium.\nBetter to modify to: try to find minimum modifications required to make sum equals goal.\n\nclass Solution {\n public
DaviLu
NORMAL
2021-03-13T22:11:46.997521+00:00
2021-03-13T22:11:46.997551+00:00
115
false
The question is too simple as medium.\nBetter to modify to: try to find minimum modifications required to make `sum` equals `goal`.\n```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n var sum=0L;\n for(int num:nums) sum+=num;\n return (int) ((Math.abs(sum-goal)+limit-1)/limit);\n }\n}\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
Simple C++ O(N) solution
simple-c-on-solution-by-indrajohn-gm6p
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n double sum = 0;\n for (int i = 0; i < nums.size(); i+
indrajohn
NORMAL
2021-03-13T09:55:47.181755+00:00
2021-03-13T09:56:38.016247+00:00
115
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n double sum = 0;\n for (int i = 0; i < nums.size(); i++) sum += nums[i];\n double diff = goal - sum;\n if (diff < 0) diff = (-1) * diff;\n int count = diff / limit;\n diff -= ((double)limit * (double)count);\n if (diff > 0) ++count;\n \n return count;\n }\n};\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
Python Greedy Solution Without using Loop
python-greedy-solution-without-using-loo-ecbs
\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s = sum(nums) \n if s == goal :\n retur
abhishek12800
NORMAL
2021-03-13T07:05:10.796016+00:00
2021-08-19T09:41:35.671696+00:00
99
false
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s = sum(nums) \n if s == goal :\n return 0 \n val = abs(goal - s)\n if val <= limit :\n return 1 \n q = val // limit \n r = val % limit \n if r == 0 :\n return q \n return q + 1 \n```
1
0
['Greedy', 'Python', 'Python3']
0
minimum-elements-to-add-to-form-a-given-sum
[JAVA] Simple to understand solution.
java-simple-to-understand-solution-by-an-29uv
\n public int minElements(int[] nums, int limit, int goal) {\n long sumOfArrayElements = 0;\n for (int i = 0; i < nums.length; i++) {\n
ankit341
NORMAL
2021-03-09T15:13:51.968875+00:00
2021-03-09T15:13:51.968922+00:00
72
false
```\n public int minElements(int[] nums, int limit, int goal) {\n long sumOfArrayElements = 0;\n for (int i = 0; i < nums.length; i++) {\n sumOfArrayElements += nums[i];\n }\n if (sumOfArrayElements == goal)\n return 0;\n\n long differenceOfGoalAndSum = Math.abs(goal - sumOfArrayElements);\n\n if (differenceOfGoalAndSum <= limit)\n return 1;\n\n long count = differenceOfGoalAndSum / limit;\n long remainder = differenceOfGoalAndSum % limit;\n\n if (remainder > 0)\n return (int)count + 1;\n\n return (int)count;\n }\n\t```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
Python easy code with explanation And one line code
python-easy-code-with-explanation-and-on-knad
1. Get target number by substracting from total sum of array to the goal\n2. Since target can be postive or negative \n3. Let take an example 10//3=3 and -10
primeAK
NORMAL
2021-03-07T07:53:31.603945+00:00
2021-03-07T10:58:35.723102+00:00
125
false
**1. Get target number by substracting from total sum of array to the goal\n2. Since target can be postive or negative \n3. Let take an example 10//3=3 and -10//3=-4 in Python\n4. So take absolute of Target number\n5. Add the quotient to the answer\n6. If there is remainder then add 1 to the answer as remainder is less than limit otherwise no need to add**\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s=sum(nums)\n target=goal-s\n ans=0\n \n ans+=abs(target)//limit\n if abs(target)%limit!=0:\n ans+=1\n return ans\n```\n\n\n# **One line code of above code is given below**\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(goal-sum(nums))/limit)\n```\n\n**Hit Upvote if you like...**
1
0
['Python']
0
minimum-elements-to-add-to-form-a-given-sum
[Python3] 3 Lines O(N)
python3-3-lines-on-by-blackspinner-hxt2
\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n total = sum(nums)\n diff = abs(total - goal)\n
blackspinner
NORMAL
2021-03-07T07:16:49.526678+00:00
2021-03-07T07:16:49.526718+00:00
40
false
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n total = sum(nums)\n diff = abs(total - goal)\n return 0 if diff == 0 else 1 if diff <= limit else diff // limit + 1 if diff % limit else diff // limit\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
java 100% I hate math!!
java-100-i-hate-math-by-trustno1-dqre
```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long total = 0, diff = 0;\n for(int n : nums) total += n;\n
trustno1
NORMAL
2021-03-07T05:19:01.111654+00:00
2021-03-07T05:19:01.111702+00:00
108
false
```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long total = 0, diff = 0;\n for(int n : nums) total += n;\n diff = Math.abs(goal - total);\n long x = diff % limit, y = diff / limit;\n return x == 0 ? (int) y : (int) (y + 1);\n }\n}
1
1
[]
0
minimum-elements-to-add-to-form-a-given-sum
python3
python3-by-aniketrastogi-587h
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s=sum(nums)\n dr=goal-s\n return math.cei
aniketrastogi
NORMAL
2021-03-07T04:58:02.100909+00:00
2021-03-07T04:58:02.100946+00:00
46
false
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s=sum(nums)\n dr=goal-s\n return math.ceil(abs(dr/limit))\n\t\t
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
easy JAVA solution 100%faster
easy-java-solution-100faster-by-smartcod-jjgi
```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long totalsum=0L;\n for(int i=0;i0 && remain>0){\n
smartcoder_06
NORMAL
2021-03-07T04:33:42.023279+00:00
2021-03-07T04:52:47.551469+00:00
125
false
```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long totalsum=0L;\n for(int i=0;i<nums.length;i++){\n totalsum+=nums[i];\n }\n if(totalsum==goal){\n return 0;\n }\n long count=0L;\n long remain=Math.abs(totalsum-(long)goal);\n if(remain<limit)return 1;\n while(limit>0 && remain>0){\n \n if(remain>limit){\n count+=remain/limit;\n remain=remain%limit;\n \n }else{\n count++;\n break;\n }\n }\n \n return (int)count;\n }\n}
1
1
['Java']
1
minimum-elements-to-add-to-form-a-given-sum
Python 1-liner
python-1-liner-by-lokeshsk1-bi5z
python []\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n\n rem = goal - sum(nums)\n\n return ceil(a
lokeshsk1
NORMAL
2021-03-07T04:15:59.609493+00:00
2024-08-04T11:41:26.334292+00:00
119
false
```python []\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n\n rem = goal - sum(nums)\n\n return ceil(abs(rem)/limit)\n \n \n```
1
0
['Python', 'Python3']
0
minimum-elements-to-add-to-form-a-given-sum
esy c++ implementation
esy-c-implementation-by-frameboy_27-qo7h
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long int s=0;\n for(int i=0;i<nums.size();i++)\n
frameboy_27
NORMAL
2021-03-07T04:12:33.036971+00:00
2021-03-07T04:12:33.037020+00:00
197
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long int s=0;\n for(int i=0;i<nums.size();i++)\n s+=nums[i];\n long long int d=abs(s-goal);\n if(d%limit==0)\n return (int)(di/limit);\n return (int)((di/limit)+1); \n }\n};\n```
1
0
['C', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
Confusing on a test case
confusing-on-a-test-case-by-timsonshi-id44
An official test case is\n\n[2,5,5,-7,4]\n7\n464680098\n\n\nFirstly I\'m confusing why the goal could be much greater than len(nums) * limit, as from the descri
timsonshi
NORMAL
2021-03-07T04:10:50.978247+00:00
2021-03-07T04:10:50.978275+00:00
90
false
An official test case is\n```\n[2,5,5,-7,4]\n7\n464680098\n```\n\nFirstly I\'m confusing why the goal could be much greater than `len(nums) * limit`, as from the description `nums` shouldn\'t be extended. Put this question aside, as `goal` is obviously greater than `len(nums) * limit`, we should modify all the elements in current `nums`, which modified **5** elements, and the current sum is 35, there is still a gap of 464680098 - 35 = 464680063. \n\nIf we can push new elements to `nums`, under the constraint that the abs of new elements shouldn\'t exceed `limit`, we should push in as many 7s as possible. Since 464680063 // 7 = 66382866, and 464680063 - 66382866 * 7 = 1, we should put in 66382866 7s and one more 1. The number of total elements to be modified is 66382866 + 1 + 5 = 66382872. But the answer is 66382870, so why?
1
0
[]
1
minimum-elements-to-add-to-form-a-given-sum
Simple math, C++
simple-math-c-by-hc167-2hmd
\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int u : nums)\n s
hc167
NORMAL
2021-03-07T04:04:07.647180+00:00
2021-03-07T04:04:07.647218+00:00
65
false
```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(int u : nums)\n sum += u;\n\n sum = abs(goal-sum);\n \n if(sum%limit == 0)\n return sum/limit;\n else\n return (sum+limit)/limit;\n }\n};\n```
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
c++ easy
c-easy-by-rakeshkr05091999-out9
class Solution {\npublic:\n int minElements(vector& nums, int limit, int goal) {\n long int sum=0;\n for(int i=0;i<nums.size();i++)\n su
rakeshkr05091999
NORMAL
2021-03-07T04:01:59.693913+00:00
2021-03-07T04:01:59.693946+00:00
100
false
class Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long int sum=0;\n for(int i=0;i<nums.size();i++)\n sum+=nums[i];\n \n long int count=0;\n if(sum==goal)\n return 0;\n sum=abs(goal-sum);\n count=sum/limit;\n if(sum%limit!=0)\n count++; \n return count;\n }\n};
1
0
[]
0
minimum-elements-to-add-to-form-a-given-sum
solved! 2line😂
solved-2line-by-azizbekeshpolatov-ukw5
Code
AzizbekEshpolatov
NORMAL
2025-03-27T09:53:45.399291+00:00
2025-03-27T09:53:45.399291+00:00
3
false
# Code ```dart [] class Solution { int minElements(List<int> nums, int limit, int goal) { int sum = nums.reduce((a,b) => a+b); return ((sum-goal).abs() / limit).ceil(); } } ```
0
0
['Dart']
0
minimum-elements-to-add-to-form-a-given-sum
0 ms Beats 100.00%
0-ms-beats-10000-by-funkeymunkey-5mii
IntuitionApproachComplexity Time complexity: Space complexity: Code
funkeymunkey
NORMAL
2025-03-25T11:15:17.960826+00:00
2025-03-25T11:15:17.960826+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElements(vector<int>& nums, int limit, int goal) { long long total = accumulate(nums.begin(),nums.end(),0LL) ; if(total == goal ) return 0 ; long long diff = abs( goal - total ) ; return (diff <= limit ) ? 1 : (diff%limit==0) ? diff/limit : diff/limit + 1 ; } }; ```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
huỳnh Văn Đương code
huynh-van-duong-code-by-3qn5nki5o5-yuv0
IntuitionApproachComplexity Time complexity: Space complexity: Code
3QN5Nki5O5
NORMAL
2025-03-01T03:09:32.426472+00:00
2025-03-01T03:09:32.426472+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElements(vector<int>& nums, int limit, int goal) { long long sum=0; for(int num:nums) { sum+=num; } if(abs(goal-sum)%limit==0) { return (abs(goal-sum)/limit); } return (abs(goal-sum)/limit)+1; } }; ```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Easy solution
easy-solution-by-liew-li-2nhr
IntuitionApproachComplexity Time complexity:O(N) Space complexity:O(1) Code
liew-li
NORMAL
2025-02-09T04:29:53.992619+00:00
2025-02-09T04:29:53.992619+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(N) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```javascript [] /** * @param {number[]} nums * @param {number} limit * @param {number} goal * @return {number} */ var minElements = function(nums, limit, goal) { let sum = 0; for (const n of nums) sum += n; let target = Math.abs(goal - sum); let res = 0; res += Math.floor(target / limit); const remain = target % limit; return remain > 0 ? res + 1 : res; }; ```
0
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
785. Minimum Elements to Add to Form a Given Sum
785-minimum-elements-to-add-to-form-a-gi-h71w
IntuitionApproachComplexity Time complexity: Space complexity: Code
G8xd0QPqTy
NORMAL
2025-01-15T03:23:41.377147+00:00
2025-01-15T03:23:41.377147+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def minElements(self, nums, limit, goal): total_sum = sum(nums) diff = abs(goal - total_sum) return (diff + limit - 1) // limit ```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
[C++ Solution (Beats 100.00%)] Min Elements | Explained
c-solution-beats-10000-min-elements-expl-ufp2
IntuitionApproachComplexity Time complexity: O(n) Space complexity: O(1) Code
daniel_9650
NORMAL
2025-01-06T23:40:50.970083+00:00
2025-01-06T23:40:50.970083+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O(n) - Space complexity: O(1) # Code ```cpp [] class Solution { public: int minElements(vector<int>& nums, int limit, int goal) { /* First, get sum of the array */ long long arr_sum = 0, diff = 0; for (auto val : nums) arr_sum += val; /* Now compute the absolute difference between the sum of * the array and the goal value. Compute how many times we can * "fit" the limit value into this difference by dividing the * difference by limit. If there is a non-zero remainder, add 1 more */ diff = std::abs(arr_sum - goal); int num_limits = (diff / limit); int remainder = (diff % limit) ? 1 : 0; return num_limits + remainder; } }; ```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Simple Java 0ms solution
simple-java-0ms-solution-by-bharani1729-gsdc
Complexity Time complexity:O(N) Space complexity:O(1) Code
bharani1729
NORMAL
2025-01-06T03:05:24.967786+00:00
2025-01-06T03:05:24.967786+00:00
10
false
# Complexity - Time complexity:O(N) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minElements(int[] nums, int limit, int goal) { long sum = 0 ; int n = nums.length; for(int num :nums)sum+=num; if(sum==goal)return 0; long req = Math.abs(goal - sum); return (req%limit ==0)? ((int) (req/limit)) : ((int) (req/limit))+1; } } ```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
Minimum Elements to Add to Form a Given Sum
minimum-elements-to-add-to-form-a-given-kr4gd
IntuitionApproachComplexity Time complexity: Space complexity: Code
Naeem_ABD
NORMAL
2025-01-04T07:32:33.504042+00:00
2025-01-04T07:32:33.504042+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```golang [] func minElements(nums []int, limit int, goal int) int { var sum int64 = 0 for _, num := range nums { sum += int64(num) } diff := math.Abs(float64(goal) - float64(sum)) return int(math.Ceil(diff / float64(limit))) } ```
0
0
['Go']
0
minimum-elements-to-add-to-form-a-given-sum
Simple iterative solution || c++ || beats 100%
simple-iterative-solution-c-beats-100-by-pjp3
IntuitionApproachComplexity Time complexity:O(n) Space complexity:O(1) Code
vikash_kumar_dsa2
NORMAL
2024-12-28T18:02:02.482778+00:00
2024-12-28T18:02:02.482778+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(n) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int minElements(vector<int>& nums, int limit, int goal) { long long sum = 0; int n = nums.size(); for(int i = 0;i<n;i++){ sum = sum + nums[i]; } long req = abs(sum - goal); if(req%limit){ return (req/limit)+1; } return req/limit; } }; ```
0
0
['Array', 'Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
C++ easy solution
c-easy-solution-by-axard-8gg0
Code
axard
NORMAL
2024-12-27T10:13:30.954537+00:00
2024-12-27T10:13:30.954537+00:00
8
false
# Code ```cpp [] class Solution { public: int minElements(vector<int>& nums, int limit, int goal) { long sum = 0; for (auto n : nums) sum += n; long d = abs(goal - sum); int count = (d / limit) + (d % limit > 0); return count; } }; ```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Nice n EZ Python Solution :D
nice-n-ez-python-solution-d-by-heterohab-gyde
Complexity\n- Time complexity:\n O(n)\n\n- Space complexity:\nO(1)\n# Code\npython3 []\nclass Solution:\n def minElements(self, nums: list[int], limit: int,
Heterohabilis_114514
NORMAL
2024-11-30T01:34:31.052809+00:00
2024-11-30T01:34:31.052842+00:00
1
false
# Complexity\n- Time complexity:\n $$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n# Code\n```python3 []\nclass Solution:\n def minElements(self, nums: list[int], limit: int, goal: int) -> int:\n return ceil(abs(goal-sum(nums))/limit)\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Straightforward Greedy Algo
straightforward-greedy-algo-by-iitjsagar-phqe
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nCalculate the diff betw
iitjsagar
NORMAL
2024-11-24T03:57:03.921412+00:00
2024-11-24T03:57:03.921434+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate the diff between goal and current sum. Number of additional elements = diff/limit\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python []\nclass Solution(object):\n def minElements(self, nums, limit, goal):\n """\n :type nums: List[int]\n :type limit: int\n :type goal: int\n :rtype: int\n """\n \n\n diff = abs(goal - sum(nums))\n\n if diff % limit == 0:\n return diff//limit\n else:\n return diff//limit + 1\n\n\n```
0
0
['Greedy', 'Python']
0
minimum-elements-to-add-to-form-a-given-sum
Find how much we need and divide that by our limit - O(n)time, O(1)space - 100% speed rank
find-how-much-we-need-and-divide-that-by-dugi
Intuition\n Describe your first thoughts on how to solve this problem. \nHow much does the limit evenly divide into the difference of the goal minus the current
OSharpe001
NORMAL
2024-11-22T16:47:43.175169+00:00
2024-11-22T20:59:23.841847+00:00
2
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHow much does the limit evenly divide into the difference of the goal minus the current sum of nums (and then add one if limit doesn\'t divide into the difference evenly).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- *Instead of adding actual numbers to nums, we can just keep track of the amount we\'re supposed to add.*\n1) Find the "target" by adding all integers in "nums" list and subtracting that sum from the "goal".\n2) Initialize another variable to zero, "additional", to represent the last possible digit needed to add to the list (that is less than limit) if the limit doesn\'t divide into our target evenly.\n3) If limit doesn\'t divide into target evenly, set additional to 1.\n4) return the sum of the amount that limit divides into the absolute value of target evenly and the additional variable (a 1 or a 0).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\nWe have to iterate through the nums list once to find the current sum before a few simple math problems.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\nWe keep track of two integers during this process\n\n# Code\n```python3 []\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n target = goal - sum(nums)\n additional = 0\n\n if abs(target) % limit > 0:\n additional += 1\n \n ans = abs(target) // limit + additional\n\n return ans\n\n```\n\n# Upon Refinement\n```python3 []\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n target = abs(goal - sum(nums))\n\n if target % limit:\n additional = 1\n else:\n additional = 0\n \n return target // limit + additional\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
beats 100%
beats-100-by-yagizerdem-eo9q
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
yagizerdem
NORMAL
2024-11-12T17:22:59.141820+00:00
2024-11-12T17:22:59.141854+00:00
1
false
# 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```javascript []\n/**\n * @param {number[]} nums\n * @param {number} limit\n * @param {number} goal\n * @return {number}\n */\nvar minElements = function (nums, limit, goal) {\n var sum = nums.reduce((a, b) => a + b);\n if (sum == goal) return 0;\n var diff = Math.abs(goal - sum);\n var step = Math.abs(limit);\n\n var counter = 0;\n\n while (diff != 0) {\n while (step > diff) {\n step--;\n }\n var skip = Math.floor(diff / step);\n diff -= step * skip;\n counter += skip;\n }\n return counter;\n};\n```
0
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
Straightforward, 4 lines
straightforward-4-lines-by-belka-u6su
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
belka
NORMAL
2024-11-04T18:54:41.028755+00:00
2024-11-04T18:54:41.028783+00:00
3
false
# 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```python3 []\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n sm=sum(nums)\n newgoal=abs(goal-sm)\n res=ceil(newgoal/limit)\n return res\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Simple java solution 100% beats
simple-java-solution-100-beats-by-phoeni-5h1v
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
gladiator-
NORMAL
2024-10-21T14:24:10.667805+00:00
2024-10-21T14:24:10.667836+00:00
15
false
# 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:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```java []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sm = 0;\n for (int num : nums) {\n sm += num; // Summing the array elements\n }\n\n long req = goal - sm; // Calculate the difference between goal and sum\n req = Math.abs(req); // Take the absolute value for required amount\n\n int ans = (int) (req / limit); // Divide by limit to get the number of full steps\n if (req % limit != 0) ans += 1; // If there\'s a remainder, add an extra step\n\n return ans;\n }\n}\n\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
Java | 4-liner | Beats %100
java-4-liner-beats-100-by-jeshuakrc-smr2
Intuition\nEvent though tagged as Greedy, you can just re-think it as: given an integer sum, being the sum of all elements in the nums arrays, and the integer r
Jeshuakrc
NORMAL
2024-10-15T04:37:05.028841+00:00
2024-10-15T04:37:05.028878+00:00
4
false
# Intuition\nEvent though tagged as Greedy, you can just re-think it as: given an integer `sum`, being the sum of all elements in the `nums` arrays, and the integer `remainder`, being the absolute value representing the distance between `sum` and `goal`; how many `limit`s would fit in `remainder`?. That\'s it! a harmless fraction.\n\n# Approach\n- Iterate over the `nums` array to find `sum`.\n- Get `abs(goal - sum)` as `remainder`.\n- Find out how many time would it be required to add `limit` to get to `remainder` (+1 if `remainder % limit != 0`).\n\n**NOTE:** Keep `sums` and `remainder` as long ints to account for large sums.\n\n# Complexity\n- Time complexity: $$O(n)$$ As per the array iteration to get `sums`.\n\n- Space complexity: $$O(1)$$\n\n# Code\n```java []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n \n long sum = 0;\n for (long n : nums) { sum += n; }\n\n long remainder = Math.abs(goal - sum);\n\n return (int) (remainder / limit) + ((remainder % limit == 0) ? 0 : 1);\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
🟢🔴🟢 Beats 100.00%👏 3 Lines Solutions 🟢🔴🟢
beats-10000-3-lines-solutions-by-develop-272s
\nclass Solution {\n int minElements(List<int> nums, int limit, int goal) {\n\n int total = nums.fold(0,(a,b)=> a+b);\n int add = goal - total;\n retu
developerSajib88
NORMAL
2024-10-11T11:56:08.118603+00:00
2024-10-11T11:56:08.118626+00:00
9
false
```\nclass Solution {\n int minElements(List<int> nums, int limit, int goal) {\n\n int total = nums.fold(0,(a,b)=> a+b);\n int add = goal - total;\n return (add.abs() / limit).ceil();\n \n }\n}\n```
0
0
['C', 'Python', 'C++', 'Java', 'JavaScript', 'C#', 'Dart']
0
minimum-elements-to-add-to-form-a-given-sum
4 Line Code
4-line-code-by-vatan999-880i
Intuition\nThe goal is to determine the minimum number of elements we need to add or subtract to transform the sum of the array nums into the goal. We can adjus
vatan999
NORMAL
2024-10-08T13:55:56.433531+00:00
2024-10-08T13:55:56.433564+00:00
1
false
# Intuition\nThe goal is to determine the minimum number of elements we need to add or subtract to transform the sum of the array `nums` into the `goal`. We can adjust the sum using values limited by a specific maximum (`limit`). The problem can be approached by first calculating the current sum and then figuring out how far we need to adjust it.\n\n# Approach\n1. **Calculate the Current Sum**: Iterate through the `nums` array to calculate the total sum of its elements.\n2. **Determine the Difference**: Compute the absolute difference between the current sum and the desired `goal`. This difference represents how much we need to adjust the sum.\n3. **Calculate Minimum Elements Needed**: To achieve the adjustment, determine how many times we can utilize the `limit` value. This can be done using the formula `(sum + limit - 1) / limit`, which gives the ceiling of the division of `sum` by `limit`. This formula effectively counts how many full increments of `limit` are required to cover the difference.\n\n# Complexity\n- **Time complexity**: $$O(n)$$, where `n` is the number of elements in the `nums` array, as we iterate through the array once to calculate the sum.\n- **Space complexity**: $$O(1)$$, since we are using a constant amount of extra space regardless of the input size.\n\n# Code\n```cpp\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for (auto i : nums) sum += i;\n sum = abs(goal - sum);\n return (sum + limit - 1) / limit;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
2 lines - 76ms
2-lines-76ms-by-8ataaxwvnn-33z9
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
8AtaAXWVNN
NORMAL
2024-10-01T02:17:25.320182+00:00
2024-10-01T02:17:25.320215+00:00
0
false
# 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```javascript []\n/**\n * @param {number[]} nums\n * @param {number} limit\n * @param {number} goal\n * @return {number}\n */\nvar minElements = function(nums, limit, goal) {\n const sum = nums.reduce((a,b) => a + b, 0)\n return Math.ceil(Math.abs(goal-sum)/limit)\n};\n```
0
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
Easy Java Solution
easy-java-solution-by-aloksharma_06-b406
Code\njava []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for(int i=0;i<nums.length;i++){\n
aloksharma_06
NORMAL
2024-09-28T19:01:12.397611+00:00
2024-09-28T19:01:12.397646+00:00
2
false
# Code\n```java []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for(int i=0;i<nums.length;i++){\n sum += nums[i];\n }\n long difference = Math.abs(sum-goal);\n if(difference == 0){\n return 0;\n }\n long min = difference / limit; \n if (difference % limit != 0) {\n min += 1; \n }\n \n return (int)min;\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
Easy than expected - Java O(N)|O(1)
easy-than-expected-java-ono1-by-wangcai2-bpq8
Intuition\n Describe your first thoughts on how to solve this problem. \nSimply follow the description, the only trick is Integer overflow, use Long for sum.\n\
wangcai20
NORMAL
2024-09-25T20:38:43.218102+00:00
2024-09-25T20:38:43.218154+00:00
5
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimply follow the description, the only trick is `Integer` overflow, use `Long` for sum.\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```java []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for (int val : nums)\n sum += val;\n return (int)(Math.abs(goal - sum) / Math.abs(limit) + (Math.abs(goal - sum) % Math.abs(limit) == 0 ? 0 : 1));\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
Easy than expected - Java
easy-than-expected-java-by-wangcai20-m7i9
Intuition\n Describe your first thoughts on how to solve this problem. \nSimply follow the description. The only trick is Integer overflow, use Long for sum.\n\
wangcai20
NORMAL
2024-09-25T20:35:43.622584+00:00
2024-09-25T20:35:43.622612+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimply follow the description. The only trick is `Integer` overflow, use `Long` for sum.\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```java []\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for (int val : nums)\n sum += val;\n return (int)(Math.abs(goal - sum) / Math.abs(limit) + (Math.abs(goal - sum) % Math.abs(limit) == 0 ? 0 : 1));\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
Simple ,Effective and Intuitive Approach to Calculate Minimum Steps in Array Problem
simple-effective-and-intuitive-approach-ptp8y
Intuition\n Describe your first thoughts on how to solve this problem. \n figure out how many elements of size limit you need to reach a goal\n\n# Approach\n De
priyanshumishragn1
NORMAL
2024-09-15T06:25:24.894754+00:00
2024-09-15T06:29:57.655994+00:00
2
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n figure out how many elements of size limit you need to reach a goal\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1)Find the Total Difference:\nFirst, calculate the difference between your goal and the sum of your current elements. This tells you how much more (or less) you need to reach the goal.\n\n2)Determine Full Steps Needed:\nDivide this difference by limit to see how many full steps of size limit are required to cover that difference.\n\n3)Check for Any Leftover:\nIf there\u2019s no remainder when dividing (i.e., the difference is exactly divisible by limit), then you just need the number of full steps calculated.\n\nIf there\u2019s a remainder, you\u2019ll need one extra step to cover the leftover part.\n\nFor Example\nif you need to cover a total difference of, say, 11 units using steps of 5 units each:\nYou would need 2 full steps (10 units).\nSince 11 - 10 = 1 unit is left, you need one more step to cover this extra unit.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nAccumulate function:O(N);\nArithmetic Operations:O(1);\n\noverall: O(N);\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=accumulate(nums.begin(),nums.end(),0LL);\n if(sum==goal) return 0;\n long long tempgoal;\n tempgoal=goal-sum;\n \n int quot=abs(tempgoal)/limit;\n if(abs(tempgoal)%limit==0) return quot;\n else return quot+1;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
2 liner || Simple maths || C++
2-liner-simple-maths-c-by-lotus18-y3fa
Code\n\nclass Solution \n{\npublic:\n int minElements(vector<int>& nums, int limit, int goal) \n {\n long long sum=accumulate(nums.begin(),nums.end
lotus18
NORMAL
2024-08-16T17:57:46.997028+00:00
2024-08-16T17:57:46.997049+00:00
1
false
# Code\n```\nclass Solution \n{\npublic:\n int minElements(vector<int>& nums, int limit, int goal) \n {\n long long sum=accumulate(nums.begin(),nums.end(),(long long)0);\n return ceil((double)abs(goal-sum)/limit);\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Python3 Math O(1)
python3-math-o1-by-dhruvp16011-oa49
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
dhruvp16011
NORMAL
2024-07-24T01:13:29.987398+00:00
2024-07-24T01:13:29.987424+00:00
1
false
# 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```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n\n s = sum(nums)\n\n req = abs(goal - s)\n if req == 0:\n return 0\n if req < limit:\n return 1\n elif req % limit == 0:\n return req //limit\n else:\n return req//limit +1 \n \n \n return 0\n\n\n \n \n\n\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Easy to understand
easy-to-understand-by-nksgbc12-5j2k
Code\n\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n \n long sum=0;\n for(int i=0; i<nums.siz
nksgbc12
NORMAL
2024-07-17T09:51:09.683276+00:00
2024-07-17T09:51:09.683312+00:00
3
false
# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n \n long sum=0;\n for(int i=0; i<nums.size(); i++) {\n sum += nums[i];\n }\n\n long req = abs(goal - sum);\n int ans = req / limit;\n if(req % limit != 0) ans++;\n return ans;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Beats 100%||O(1) TC and SC
beats-100o1-tc-and-sc-by-xbhinxv54-xfff
Intuition\n If sum of the nums is already equal to goal,no need to add anything so return 0\nElse if sum is less than goal we need to add some elements,the sum
xbhinxv54
NORMAL
2024-06-21T09:22:42.168194+00:00
2024-06-21T09:22:42.168229+00:00
15
false
# Intuition\n<!-- If sum of the nums is already equal to goal,no need to add anything so return 0\nElse if sum is less than goal we need to add some elements,the sum of the elements to be added will be the difference between the goal and the sum of all elements in the current list. If this sum is less than the limit just return 1 as the difference can be added as one element -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- O(1) -->\n\n- Space complexity:\n<!-- O(1) -->\n\n# Code\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n if sum(nums)==goal:\n return 0\n x=goal-sum(nums)\n if abs(x)<limit:\n return 1\n count=0\n count+=math.ceil(abs(x)/limit)\n return count\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Python Medium
python-medium-by-lucasschnee-y570
\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s = sum(nums)\n\n left = s - goal\n\n if lef
lucasschnee
NORMAL
2024-06-16T14:35:05.466789+00:00
2024-06-16T14:35:05.466813+00:00
3
false
```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n s = sum(nums)\n\n left = s - goal\n\n if left == 0:\n return 0\n\n\n left = abs(left)\n\n\n amount = left // limit\n\n left %= limit\n if left:\n amount += 1\n\n return amount \n \n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Easy C++ Soln || Simple subtraction || TC ->O(N) || SC->O(1)
easy-c-soln-simple-subtraction-tc-on-sc-8k6gs
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
Xpider_7080
NORMAL
2024-06-14T04:59:25.387899+00:00
2024-06-14T04:59:25.387924+00:00
3
false
# 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```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=accumulate(nums.begin(),nums.end(),0*1LL);\n cout<<sum<<" ";\n long long diff=goal-sum;\n diff=abs(diff);\n long long ans=0;\n while(diff>0){\n ans++;\n diff-=limit;\n }\n\n return ans;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Easy Greedy intuitive
easy-greedy-intuitive-by-afrid85-x12s
\n# Code\n\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n //int sum=0; \n long long sum=0; \n
afrid85
NORMAL
2024-06-05T05:28:26.917563+00:00
2024-06-05T05:28:26.917602+00:00
2
false
\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n //int sum=0; \n long long sum=0; \n for(int i=0;i<nums.size();i++){\n sum +=nums[i];\n }\n if(sum == goal)return 0; \n long long needed = goal-sum; \n needed = abs(needed);\n if(needed%limit == 0)return needed/limit; \n \n return needed/limit+1; \n }\n};\n```
0
0
['Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
😊Runtime 76 ms Beats 100.00% of users with Go
runtime-76-ms-beats-10000-of-users-with-b5sfv
Code\n\nfunc minElements(nums []int, limit int, goal int) int {\n\tvar sum int64 = 0\n\tfor _, num := range nums {\n\t\tsum += int64(num)\n\t}\n\n\tdiff := math
pvt2024
NORMAL
2024-05-25T02:23:58.971907+00:00
2024-05-25T02:23:58.971975+00:00
1
false
# Code\n```\nfunc minElements(nums []int, limit int, goal int) int {\n\tvar sum int64 = 0\n\tfor _, num := range nums {\n\t\tsum += int64(num)\n\t}\n\n\tdiff := math.Abs(float64(goal) - float64(sum))\n\treturn int(math.Ceil(diff / float64(limit)))\n}\n```
0
0
['Go']
0
minimum-elements-to-add-to-form-a-given-sum
C++ SOLUTION
c-solution-by-shantanuubasu-x01z
Approach\n Describe your approach to solving the problem. \n1. Find the sum of array.\n2. Calculate its absolute difference from the goal.\n3. Then return the c
shantanuubasu
NORMAL
2024-05-13T12:58:55.866085+00:00
2024-05-13T12:58:55.866111+00:00
0
false
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Find the sum of array.\n2. Calculate its absolute difference from the goal.\n3. Then return the ceil of diff/limit.\n\nGitHub: [1785. Minimum Elements to Add to Form a Given Sum](https://github.com/Shantanuubasu/Leetcode/blob/main/Array/1785.%20Minimum%20Elements%20to%20Add%20to%20Form%20a%20Given%20Sum.cpp)\n\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(0);\n\n long sum=accumulate(nums.begin(),nums.end(),0L);\n long diff=abs(goal-sum);\n return ceil((double)diff/limit);\n }\n};\n```
0
0
['Array', 'Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
2 liner || GREEDY
2-liner-greedy-by-rastogimudit02-831q
Intuition\nFind diff between sum of array and goal.\nNow, in order to make both of them same, we need to +/-\nnumbers in array such that sum = goal.\nTo get min
coder_rastogi_21
NORMAL
2024-05-09T01:08:26.395161+00:00
2024-05-09T01:08:26.395183+00:00
2
false
# Intuition\n`Find diff between sum of array and goal.`\n`Now, in order to make both of them same, we need to +/-`\n`numbers in array such that sum = goal.`\n`To get minimum steps,`\n`we keep on doing +/- limit to make sum = goal` \n`as early as possible.`\n\n# Complexity\n- Time complexity: $$O(n)$$ \n\n- Space complexity: $$O(1)$$ \n\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = accumulate(nums.begin(),nums.end(),0LL);\n return ceil(1.00*abs(sum-goal)/limit);\n }\n};\n```
0
0
['Greedy', 'C++']
0
minimum-elements-to-add-to-form-a-given-sum
❇ minimum-elements-to-add👌 🏆O(N)❤️ Javascript🎯 Memory👀95.45%🕕 ++Explanation✍️ 🔴🔥 ✅ 👉 💪🙏
minimum-elements-to-add-on-javascript-me-7g3u
Time Complexity: O(N)\nSpace Complexity: O(1)\n\n\nvar minElements = function (nums, limit, goal) {\n const sum = (function () {\n let sum = 0;\n
anurag-sindhu
NORMAL
2024-04-25T02:29:53.123950+00:00
2024-04-25T02:29:53.123972+00:00
1
false
Time Complexity: O(N)\nSpace Complexity: O(1)\n\n```\nvar minElements = function (nums, limit, goal) {\n const sum = (function () {\n let sum = 0;\n for (const iterator of nums) {\n sum += iterator;\n }\n return sum;\n })();\n let diff = 0;\n if ((sum < 0 && goal < 0) || (sum >= 0 && goal >= 0)) {\n diff += Math.abs(sum - goal);\n } else if (sum < 0 && goal > 0) {\n diff += Math.abs(sum) + goal;\n } else if (sum >= 0 && goal < 0) {\n diff += Math.abs(goal) + sum;\n }\n return Math.ceil(diff / Math.abs(limit));\n};\n```
0
0
['JavaScript']
0
minimum-elements-to-add-to-form-a-given-sum
Swift
swift-by-ksmirnovnn-giqh
First calculating the sum of the array. Then calculate the absolute difference between the goal and the sum. This difference is the amount that needs to be adde
ksmirnovnn
NORMAL
2024-04-21T11:17:11.728898+00:00
2024-04-21T11:17:11.728929+00:00
3
false
First calculating the sum of the array. Then calculate the absolute difference between the goal and the sum. This difference is the amount that needs to be added to the array to reach the goal. Since each added element can have an absolute value of up to limit, the minimum number of elements needed is the difference divided by limit. The expression (diff + limit - 1) / limit is equivalent to ceil(diff / limit), but it avoids the need for floating-point division.\n\n# Code\n```\nclass Solution {\n func minElements(_ nums: [Int], _ limit: Int, _ goal: Int) -> Int {\n let sum = nums.reduce(0, +)\n let diff = abs(goal - sum)\n return (diff + limit - 1) / limit\n }\n}\n\n```
0
0
['Swift']
0
minimum-elements-to-add-to-form-a-given-sum
Beats 100% Time Complexity | Java | Arithmetic Solution |
beats-100-time-complexity-java-arithmeti-8s5h
Intuition\n Describe your first thoughts on how to solve this problem. \nThe Intuition here is that we need to find how many times we need to use limit to get t
code_bandit
NORMAL
2024-04-19T14:59:53.371922+00:00
2024-04-19T14:59:53.371949+00:00
42
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe Intuition here is that we need to find how many times we need to use limit to get to the difference of current array sum and the goal.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Calculate the array sum.\n- Get the absolute difference between sum and goal - we take absolute because sum can be bigger or smaller than goal, if sum is smaller then we need to add and if it\'s greater then we need to subtract to reach goal but regardless we will subtract or add limit to sum.\n- Divide difference of sum and goal with limit to get the number of times we need to use limit.\n- If diff is exactly divisible by limit then that\'s the answer else add 1 to the answer.\n\nPlease **UPVOTE** if you find helpfull.\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n int n = nums.length;\n int res = 0;\n long sum = 0;\n long lim = (long) limit;\n long goall = (long) goal;\n for(int i=0; i<n; i++) sum += nums[i];\n // System.out.println(sum);\n long diff = Math.abs(sum-goall);\n return diff%limit == 0L? (int)(diff/limit) : (int)(diff/limit)+1;\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
scala oneliner
scala-oneliner-by-vititov-ggyb
\nobject Solution {\n def minElements(nums: Array[Int], limit: Int, goal: Int): Int =\n (((goal - nums.foldLeft(0L)(_ + _)).abs + limit - 1) / limit).toInt\
vititov
NORMAL
2024-04-11T17:52:34.409692+00:00
2024-04-11T17:52:34.409726+00:00
1
false
```\nobject Solution {\n def minElements(nums: Array[Int], limit: Int, goal: Int): Int =\n (((goal - nums.foldLeft(0L)(_ + _)).abs + limit - 1) / limit).toInt\n}\n```
0
0
['Scala']
0
minimum-elements-to-add-to-form-a-given-sum
JS || Solution by Bharadwaj
js-solution-by-bharadwaj-by-manu-bharadw-0e0f
Code\n\nvar minElements = (nums, limit, goal) => Math.ceil(Math.abs(nums.reduce((prev, cur) => prev - cur, goal) / limit));\n
Manu-Bharadwaj-BN
NORMAL
2024-03-01T05:59:37.555663+00:00
2024-03-01T05:59:37.555687+00:00
6
false
# Code\n```\nvar minElements = (nums, limit, goal) => Math.ceil(Math.abs(nums.reduce((prev, cur) => prev - cur, goal) / limit));\n```
0
0
['JavaScript']
1
minimum-elements-to-add-to-form-a-given-sum
Ruby O(k)
ruby-ok-by-tmpasipanodya-rbo5
Intuition\nNot sure why this is labelled as a medium.\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(k)\n
tmpasipanodya
NORMAL
2024-02-29T20:43:01.226976+00:00
2024-02-29T20:43:01.227007+00:00
3
false
# Intuition\nNot sure why this is labelled as a medium.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(k)\n\n- Space complexity: O(k)\n\n# Code\n```\n# @param {Integer[]} nums\n# @param {Integer} limit\n# @param {Integer} goal\n# @return {Integer}\ndef min_elements(nums, limit, goal)\n sum = nums.sum\n difference = goal - sum\n\n return 0 if difference == 0\n\n return 1 if difference.abs <= limit\n\n (difference.abs / limit) + [(difference.abs % limit), 1].min\n\nend\n```
0
0
['Ruby']
0
minimum-elements-to-add-to-form-a-given-sum
Simple 3 Lines C++
simple-3-lines-c-by-elcabalero-v2qc
Intuition + Approach\n Describe your first thoughts on how to solve this problem. \nThe intuition is to compare the initial sum of the array\'s elements s with
elcabalero
NORMAL
2024-02-14T18:24:11.841698+00:00
2024-02-14T18:24:11.841728+00:00
4
false
# Intuition + Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to compare the initial sum of the array\'s elements `s` with the target sum `goal`. Then, since we know how big (or small) can each integer be, we can compute the missing amount of integers to insert with a simple formula.\n\nThis is approach is correct since we need to find the smallest amount of integers to add to the array, so the most convenient choice to do is to add the maximum integer `limit` for (`missing`/`limit`) times, and, if we still haven\'t reached the target `goal`, then one more integer (which would have the value `goal`-`missing`/`limit`).\n\n# Complexity\n- Time complexity: $$O(n)$$ for the sum.\n- Space complexity: $$O(1)$$.\n\n# Code\n```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long s = accumulate(nums.begin(), nums.end(), 0LL);\n long long missing = abs(goal - s);\n return (missing + limit - 1) / limit; \n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
EASY to understand @beats 90-100%
easy-to-understand-beats-90-100-by-dash1-ktmd
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n first find total sum
dash123---
NORMAL
2024-02-10T18:58:10.957283+00:00
2024-02-10T18:58:10.957298+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n first find total sum and check that if diffrence between goal and sum is divisible by limit if it divisible means we required the number of additional number equal to our quotient else it equal to quotient+1.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n# Code\n```\nclass Solution {\npublic:\n \n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum=0;long int count=0;\n for(int i=0;i<nums.size();i++)\n {\n sum=sum+nums[i];\n }\n long long req=goal-sum;\n if(abs(req)%abs(limit)!=0)\n count=1;\n count=count+abs(req)/abs(limit);\n \n return count;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
100%
100-by-devziv-59vv
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
DevZiv
NORMAL
2024-02-04T15:58:13.813865+00:00
2024-02-04T15:58:13.813895+00:00
9
false
# 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:o(n)\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```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n import math\n return math.ceil(abs(goal-sum(nums))/limit)\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Easy C++ Solution
easy-c-solution-by-nimbalkarbhagyesh6979-8hgt
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
nimbalkarbhagyesh6979
NORMAL
2024-01-31T07:35:57.308232+00:00
2024-01-31T07:35:57.308268+00:00
1
false
# 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```\nclass Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n int minElements(vector<int>& nums, int limit, int goal) {\n long long int Sum = 0;\n for(int i=0;i<nums.size();i++) Sum+=nums[i];\n if(Sum==goal) return 0;\n long long int diff = abs(Sum-goal);\n if(diff<limit) return 1;\n return (diff%limit==0)?diff/limit:diff/limit+1;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
Dart Solution
dart-solution-by-friendlygecko-p2zk
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
Friendlygecko
NORMAL
2024-01-17T21:21:28.165079+00:00
2024-01-17T21:21:28.165104+00:00
0
false
# 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```\nclass Solution {\n int minElements(List<int> nums, int limit, int goal) {\n final sum = nums.fold<int>(0, (a, b) => a + b);\n final diff = (sum - goal).abs();\n return (diff / limit).ceil();\n }\n}\n\n```
0
0
['Dart']
0
minimum-elements-to-add-to-form-a-given-sum
RUNTIME 75% .... MEMOERY 100%,,,EASIEST C++ SOLUTION..
runtime-75-memoery-100easiest-c-solution-n66n
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
DevRuhela
NORMAL
2024-01-05T17:19:38.835921+00:00
2024-01-05T17:19:38.835968+00:00
5
false
# 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```\nclass Solution {\npublic:\n int minElements(vector<int>& nums, int limit, int goal) {\n long long sum = 0;\n for(auto i : nums){\n sum += i;\n }\n \n long long l = limit;\n long long g = goal;\n if(sum == g) return 0;\n int ans = 0;\n long long x = abs(goal - sum);\n if(limit >= x) return 1;\n else{\n ans = x/limit;\n if(x % limit != 0) ans++; \n }\n return ans;\n }\n};\n```
0
0
['C++']
0
minimum-elements-to-add-to-form-a-given-sum
python3 1 liner (beats 100%)
python3-1-liner-beats-100-by-0icy-e9rj
\n\n# Code\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(sum(nums)-goal)/limit)\n
0icy
NORMAL
2023-12-25T09:03:01.959395+00:00
2023-12-25T09:03:01.959427+00:00
8
false
\n\n# Code\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return ceil(abs(sum(nums)-goal)/limit)\n```
0
0
['Python3']
0
minimum-elements-to-add-to-form-a-given-sum
Java 4 lines
java-4-lines-by-fredo30400-7cca
\n\n# Code\n\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for (int x:nums){sum+=x;}\n
fredo30400
NORMAL
2023-11-27T13:56:52.798005+00:00
2023-11-27T13:56:52.798033+00:00
66
false
\n\n# Code\n```\nclass Solution {\n public int minElements(int[] nums, int limit, int goal) {\n long sum = 0;\n for (int x:nums){sum+=x;}\n long toFind = Math.abs(goal-sum);\n return (int)(toFind/limit) + (toFind % limit !=0 ? 1 : 0);\n }\n}\n```
0
0
['Java']
0
minimum-elements-to-add-to-form-a-given-sum
One line Python answer
one-line-python-answer-by-dhaxina-xva8
Code\n\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return abs(sum(nums)-goal)//limit if abs(sum(nums)-g
Dhaxina
NORMAL
2023-11-21T11:46:24.418606+00:00
2023-11-21T11:46:24.418636+00:00
6
false
# Code\n```\nclass Solution:\n def minElements(self, nums: List[int], limit: int, goal: int) -> int:\n return abs(sum(nums)-goal)//limit if abs(sum(nums)-goal)%limit==0 else abs(sum(nums)-goal)//limit+1\n```
0
0
['Python', 'Python3']
0
smallest-subsequence-of-distinct-characters
[Java/C++/Python] Stack Solution O(N)
javacpython-stack-solution-on-by-lee215-gv7u
Explanation:\nFind the index of last occurrence for each character.\nUse a stack to keep the characters for result.\nLoop on each character in the input string
lee215
NORMAL
2019-06-09T04:03:55.772188+00:00
2019-11-29T15:30:57.397973+00:00
29,896
false
## **Explanation**:\nFind the index of last occurrence for each character.\nUse a `stack` to keep the characters for result.\nLoop on each character in the input string `S`,\nif the current character is smaller than the last character in the stack,\nand the last character exists in the following stream,\nwe can pop the last character to get a smaller result.\n<br>\n\n## **Time Complexity**:\nTime `O(N)`\nSpace `O(26)`\n\n<br>\n\n**Java:**\n```java\n public String smallestSubsequence(String S) {\n Stack<Integer> stack = new Stack<>();\n int[] last = new int[26], seen = new int[26];\n for (int i = 0; i < S.length(); ++i)\n last[S.charAt(i) - \'a\'] = i;\n for (int i = 0; i < S.length(); ++i) {\n int c = S.charAt(i) - \'a\';\n if (seen[c]++ > 0) continue;\n while (!stack.isEmpty() && stack.peek() > c && i < last[stack.peek()])\n seen[stack.pop()] = 0;\n stack.push(c);\n }\n StringBuilder sb = new StringBuilder();\n for (int i : stack) sb.append((char)(\'a\' + i));\n return sb.toString();\n }\n```\n\n**C++**\n```cpp\n string smallestSubsequence(string s) {\n string res = "";\n int last[26] = {}, seen[26] = {}, n = s.size();\n for (int i = 0; i < n; ++i)\n last[s[i] - \'a\'] = i;\n for (int i = 0; i < n; ++i) {\n if (seen[s[i] - \'a\']++) continue;\n while (!res.empty() && res.back() > s[i] && i < last[res.back() - \'a\'])\n seen[res.back() - \'a\'] = 0, res.pop_back();\n res.push_back(s[i]);\n }\n return res;\n }\n```\n\n**Python:**\n```py\n def smallestSubsequence(self, S):\n last = {c: i for i, c in enumerate(S)}\n stack = []\n for i, c in enumerate(S):\n if c in stack: continue\n while stack and stack[-1] > c and i < last[stack[-1]]:\n stack.pop()\n stack.append(c)\n return "".join(stack)\n```\n<br>\n\n# More Good Stack Problems\nHere are some problems that impressed me.\nGood luck and have fun.\n\n- 1130. [Minimum Cost Tree From Leaf Values](https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space)\n- 907. [Sum of Subarray Minimums](https://leetcode.com/problems/sum-of-subarray-minimums/discuss/170750/C++JavaPython-Stack-Solution)\n- 901. [Online Stock Span](https://leetcode.com/problems/online-stock-span/discuss/168311/C++JavaPython-O(1))\n- 856. [Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/discuss/141777/C++JavaPython-O(1)-Space)\n- 503. [Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/discuss/98270/JavaC++Python-Loop-Twice)\n- 496. Next Greater Element I\n- 84. Largest Rectangle in Histogram\n- 42. Trapping Rain Water\n<br>
425
5
[]
42
smallest-subsequence-of-distinct-characters
C++ O(n), identical to #316
c-on-identical-to-316-by-votrubac-kpte
The solution is exactly the same as for 316. Remove Duplicate Letters.\n\nFrom the input string, we are trying to greedily build a monotonically increasing resu
votrubac
NORMAL
2019-06-09T04:01:40.320484+00:00
2019-06-12T16:57:41.342318+00:00
12,879
false
The solution is exactly the same as for [316. Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters).\n\nFrom the input string, we are trying to greedily build a monotonically increasing result string. If the next character is smaller than the back of the result string, we remove larger characters from the back **providing** there are more occurrences of that character **later** in the input string.\n```\nstring smallestSubsequence(string s, string res = "") {\n int cnt[26] = {}, used[26] = {};\n for (auto ch : s) ++cnt[ch - \'a\'];\n for (auto ch : s) {\n --cnt[ch - \'a\'];\n if (used[ch - \'a\']++ > 0) continue;\n while (!res.empty() && res.back() > ch && cnt[res.back() - \'a\'] > 0) {\n used[res.back() - \'a\'] = 0;\n res.pop_back();\n }\n res.push_back(ch);\n }\n return res;\n} \n```\n## Complexity Analysis\nRuntime: O(n). We process each input character no more than 2 times.\nMemory: O(1). We need the constant memory to track up to 26 unique letters.
135
1
[]
15