id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n vector<bool> used;\n int operationNum = 0;\n int length = nums.size();\n for (int i = 0; i < length; i++){\n used.push_back(false);\n }\n sort(nums.begin(), nums.end());\n int i = 0;\n int j = length - 1;\n int firstRider;\n int secondRider;\n int sum;\n while (i < j){\n firstRider = nums[i];\n secondRider = nums[j];\n printf(\"%d %d\\n\", firstRider, secondRider);\n if (used[i]){\n i++;\n continue;\n }\n if (used[j]){\n j--;\n continue;\n }\n if (firstRider >= k){\n i++;\n continue;\n }\n if (secondRider >= k){\n j--;\n continue;\n }\n sum = firstRider + secondRider;\n if (sum == k) {\n operationNum++;\n used[i] = true;\n used[j] = true;\n i++;\n j--;\n continue;\n }\n else if (sum < k){\n i++;\n continue;\n }\n else{\n j--;\n }\n }\n return operationNum;\n }\n};",
"memory": "62121"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n const int fastio = [](){ios::sync_with_stdio(false); cin.tie(nullptr); return 0;}();\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n\n int left = 0, right = nums.size() - 1;\n int count = 0;\n while (left < right) {\n const int sum = nums[left] + nums[right];\n if (sum < k)\n left += 1;\n else if (sum > k)\n right -= 1;\n else {\n left += 1;\n right -= 1;\n count += 1;\n }\n }\n return count;\n }\n};",
"memory": "62338"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "auto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n int maxOperations(vector<int>& nums, int k)const noexcept {\n sort(nums.begin(),nums.end());\n int i=0;int j=nums.size()-1;\n int count=0;\n while(i<j){\n if(nums[i]+nums[j]==k){\n count++;\n i++;\n j--;\n }\n else if((nums[i]+nums[j])<k){\n i++;\n }\n else{\n j--;\n }\n }\n return count;\n }\n};",
"memory": "62338"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n std::ios_base::sync_with_stdio(false);\n\t std::cin.tie(nullptr); \n sort(nums.begin(), nums.end());\n int i=0, j = nums.size()-1, ans=0;\n\n while(i<j){\n auto sum = nums[i]+nums[j];\n if(sum == k){\n i++;j--;\n ans++;\n }\n else if(sum <k){\n i++;\n }\n else{\n j--;\n }\n }\n\n return ans;\n }\n};",
"memory": "62554"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false); \n cin.tie(NULL); \n cout.tie(NULL);\n sort(nums.begin(), nums.end());\n int count = 0;\n int left = 0, right = nums.size()-1;\n while(right>left){\n \n if(nums[right] + nums[left] == k){\n left++;\n right--;\n count++;\n }\n else if(nums[right] + nums[left] > k){\n right--;\n }\n else if(nums[right] + nums[left] < k){\n left++;\n }\n }\n return count;\n }\n};",
"memory": "62554"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false); \n cin.tie(NULL); \n cout.tie(NULL);\n sort(nums.begin(), nums.end());\n int count = 0;\n int left = 0, right = nums.size()-1;\n while(right>left){\n \n if(nums[right] + nums[left] == k){\n left++;\n right--;\n count++;\n }\n else if(nums[right] + nums[left] > k){\n right--;\n }\n else if(nums[right] + nums[left] < k){\n left++;\n }\n }\n return count;\n }\n};",
"memory": "62770"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int count=0;\n auto radixSort = [](std::vector<int>& arr) {\n int max_num = *std::max_element(arr.begin(), arr.end());\n int exp = 1;\n std::vector<int> output(arr.size());\n \n while (max_num / exp > 0) {\n std::vector<int> count(10, 0);\n\n for (int i = 0; i < arr.size(); i++) {\n count[(arr[i] / exp) % 10]++;\n }\n\n for (int i = 1; i < 10; i++) {\n count[i] += count[i - 1];\n }\n\n for (int i = arr.size() - 1; i >= 0; i--) {\n output[count[(arr[i] / exp) % 10] - 1] = arr[i];\n count[(arr[i] / exp) % 10]--;\n }\n\n for (int i = 0; i < arr.size(); i++) {\n arr[i] = output[i];\n }\n\n exp *= 10;\n }\n };\n radixSort(nums);\n int left=0;\n int right=nums.size()-1;\n while (left<right)\n {\n if(nums[left]+nums[right]==k)\n {\n count++;\n left++;\n right--;\n }\n else if (nums[left]+nums[right]<k)\n {\n left++;\n }\n else\n {\n right--;\n }\n }\n return count;\n}\n \n};",
"memory": "62770"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int count=0;\n auto radixSort = [](std::vector<int>& arr) {\n int max_num = *std::max_element(arr.begin(), arr.end());\n int exp = 1;\n std::vector<int> output(arr.size());\n \n while (max_num / exp > 0) {\n std::vector<int> count(10, 0);\n\n for (int i = 0; i < arr.size(); i++) {\n count[(arr[i] / exp) % 10]++;\n }\n\n for (int i = 1; i < 10; i++) {\n count[i] += count[i - 1];\n }\n\n for (int i = arr.size() - 1; i >= 0; i--) {\n output[count[(arr[i] / exp) % 10] - 1] = arr[i];\n count[(arr[i] / exp) % 10]--;\n }\n\n for (int i = 0; i < arr.size(); i++) {\n arr[i] = output[i];\n }\n\n exp *= 10;\n }\n };\n radixSort(nums);\n int left=0;\n int right=nums.size()-1;\n while (left<right)\n {\n if(nums[left]+nums[right]==k)\n {\n count++;\n left++;\n right--;\n }\n else if (nums[left]+nums[right]<k)\n {\n left++;\n }\n else\n {\n right--;\n }\n }\n return count;\n}\n \n};",
"memory": "62986"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int count=0;\n auto radixSort = [](vector<int>& arr) {\n int max_num = *max_element(arr.begin(), arr.end());\n int exp = 1;\n vector<int> output(arr.size());\n \n while (max_num / exp > 0) {\n vector<int> count(10, 0);\n for (int i = 0; i < arr.size(); i++) {\n count[(arr[i] / exp) % 10]++;\n }\n for (int i = 1; i < 10; i++) {\n count[i] += count[i - 1];\n }\n for (int i = arr.size() - 1; i >= 0; i--) {\n output[count[(arr[i] / exp) % 10] - 1] = arr[i];\n count[(arr[i] / exp) % 10]--;\n }\n\n for (int i = 0; i < arr.size(); i++) {\n arr[i] = output[i];\n }\n\n exp *= 10;\n }\n };\n radixSort(nums);\n int left=0;\n int right=nums.size()-1;\n while (left<right)\n {\n if(nums[left]+nums[right]==k)\n {\n count++;\n left++;\n right--;\n }\n else if (nums[left]+nums[right]<k)\n {\n left++;\n }\n else\n {\n right--;\n }\n }\n return count;\n}\n \n};",
"memory": "62986"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n [[nodiscard]]\n auto maxOperations( vector<int> nums, const int k) const\n -> int\n {\n if (nums.size() < 2) return 0;\n\n int max { 0 };\n\n std::ranges::sort(nums);\n int left { 0 };\n int right { static_cast<int>(nums.size() - 1) };\n \n while (left < right)\n {\n const auto sum = nums[left] + nums[right];\n if (sum == k)\n {\n ++left;\n --right;\n ++max;\n }\n else if (sum < k)\n {\n ++left;\n }\n else\n {\n --right;\n }\n }\n\n\n return max;\n }\n};",
"memory": "63203"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n vector<int> final = nums;\n int left = 0; int right = nums.size()-1;\n int temp = 0;\n while(left < right){\n if(final[left] + final[right] == k){\n left++;\n right--;\n temp++;\n } else if(final[left] + final[right] < k){\n left++;\n } else {\n right--;\n }\n }\n return temp;\n }\n};",
"memory": "64500"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int left=0,right=nums.size()-1;\n vector<int> index(nums.size(),0);\n int count=0;\n sort(nums.begin(),nums.end());\n while(left<right){\n int sum=nums[left]+nums[right];\n cout<<\"Sum: \"<<sum<<endl;\n if(sum==k){\n index[left]=1;\n index[right]=1;\n left++;\n right--;\n count++;\n cout<<\"count: \"<<count<<endl;\n }\n else{\n\n if(sum<k){\n left++;\n }\n else{\n right--;\n }\n }\n cout<<\"left: \"<<left<<\"right: \"<<right<<endl;\n }\n return count;\n \n }\n};",
"memory": "64500"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n\n vector<pair<int,int>>vec;\n\n int cnt = 1;\n int ele = nums[0];\n\n for(int i=1; i<n; i++){\n if(nums[i]==ele) cnt++;\n else{\n vec.push_back({ele, cnt});\n ele = nums[i];\n cnt = 1;\n }\n }\n vec.push_back({ele, cnt});\n\n int ans = 0;\n\n int l = 0, r = vec.size()-1;\n\n while(l<=r){\n if(vec[l].first + vec[r].first > k){\n r--;\n }\n else if(vec[l].first + vec[r].first < k){\n l++;\n }\n else{\n if(l!=r){\n ans += min(vec[l].second, vec[r].second);\n }\n else{\n ans += vec[l].second/2;\n }\n l++;\n r--;\n }\n\n }\n\n return ans;\n\n\n\n }\n};",
"memory": "64716"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n int count=0;\n for(int ele:nums){\n if(ele<=k){\n m[ele]++;\n }\n }\n for(auto ele:m){\n int n=k-ele.first;\n if(m.find(n)!=m.end()){\n if(n==ele.first){\n count+=ele.second/2;\n }\n else count+=min(m[n],ele.second);\n m[n]=0;\n m[ele.first]=0;\n \n }\n }\n return count;\n }\n};",
"memory": "64933"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n // 1. remove num > k\n // 2. build num -> count hash table.\n // 3. iterate the hash table to count pairs.\n\n unordered_map<int, int> m; // num -> count\n int operations = 0;\n\n for (auto num : nums) {\n if (num <= k) m[num]++;\n }\n \n for (auto [num, cnt] : m) {\n if (num == k) continue; // the number which equals k can't be one of pair\n\n int remaining = k - num;\n if (m.find(remaining) == m.end()) continue;\n\n int pairCnt = num == remaining ? m[num] / 2 : min(m[num], m[remaining]);\n operations += pairCnt;\n m[num] -= pairCnt;\n m[remaining] -= pairCnt;\n }\n\n return operations;\n }\n};",
"memory": "65149"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n // 1. remove num > k\n // 2. build num -> count hash table.\n // 3. iterate the hash table to count pairs.\n\n unordered_map<int, int> m; // num -> count\n int operations = 0;\n\n for (auto num : nums) {\n // the number which larger or equals than k can't be one of pair\n if (num < k) m[num]++;\n }\n \n for (auto [num, cnt] : m) {\n // No one can be current number's pair\n int remaining = k - num;\n if (m.find(remaining) == m.end()) continue;\n\n // pair count from the same number (ex: 3 + 3 = 6) or\n // different numbers (ex: 2 + 4 = 6)\n int pairCnt = num == remaining ? m[num] / 2 : min(m[num], m[remaining]);\n operations += pairCnt;\n m[num] -= pairCnt;\n m[remaining] -= pairCnt;\n }\n\n return operations;\n }\n};",
"memory": "65149"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n if (nums.size() < 2) return 0;\n unordered_map<int, int> occur;\n for (int num : nums) {\n if (num >= k) continue;\n if (occur.contains(num)) {\n occur[num] ++;\n } else {\n occur[num] = 1;\n }\n }\n\n int result = 0;\n\n for (auto p : occur) {\n int key = p.first;\n int count = p.second;\n if (k % 2 == 0 && key == k / 2) {\n result += count / 2;\n } else if (key <= k / 2) {\n int key2 = k - key;\n int count2 = occur.contains(key2) ? occur[key2] : 0;\n result += min(count, count2);\n } else {\n // skip. already counted from the other side.\n } \n }\n return result;\n }\n};",
"memory": "65365"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n unordered_map<int, int> count; // num, number of occurences\n int opp_count = 0;\n\n for (int& x : nums){\n if (x > k) continue;\n count[x]++;\n }\n\n for (int& x : nums){\n if (count.find(k-x) != count.end()){ // if k-x exists, possible pair\n if (k%2 == 0 && x == k/2){ // if x = k/2 then check if there is more than 1 to pair with\n int num_pairs = floor(count[x]/2); // could be 0 even if count[x] = 1\n opp_count += num_pairs;\n count[x] -= num_pairs * 2;\n } else {\n int num_pairs = min(count[x],count[k-x]); // could be 0 (no pairs)\n opp_count += num_pairs;\n count[x] -= num_pairs;\n count[k-x] -= num_pairs;\n } \n }\n }\n\n return opp_count;\n }\n};",
"memory": "65365"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> hash_table;\n int size = nums.size();\n int count = 0;\n \n for(int i= 0;i<size;i++)\n {\n if(nums[i]< k)\n {\n if(hash_table[k-nums[i]] == 0)\n {\n hash_table[nums[i]]++;\n }\n else\n {\n count++;\n hash_table[k-nums[i]]--;\n }\n }\n }\n return count;\n\n ///// memory limit exceeded\n // vector<int> v(k,0);\n // int size = nums.size();\n // int count = 0;\n \n // for(int i= 0;i<size;i++)\n // {\n // if(nums[i]< k)\n // {\n // if(v[k-nums[i]] == 0)\n // {\n // v[nums[i]]++;\n // }\n // else\n // {\n // count++;\n // v[k-nums[i]]--;\n // }\n // }\n // }\n //return count;\n \n }\n};",
"memory": "65581"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> hash_table;\n int size = nums.size();\n int count = 0;\n \n for(int i= 0;i<size;i++)\n {\n if(nums[i]< k)\n {\n if(hash_table[k-nums[i]] == 0)\n {\n hash_table[nums[i]]++;\n }\n else\n {\n count++;\n hash_table[k-nums[i]]--;\n }\n }\n }\n return count;\n\n ///// memory limit exceeded\n // vector<int> v(k,0);\n // int size = nums.size();\n // int count = 0;\n \n // for(int i= 0;i<size;i++)\n // {\n // if(nums[i]< k)\n // {\n // if(v[k-nums[i]] == 0)\n // {\n // v[nums[i]]++;\n // }\n // else\n // {\n // count++;\n // v[k-nums[i]]--;\n // }\n // }\n // }\n //return count;\n \n }\n};",
"memory": "65581"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int K) {\n map<int, int> m;\n for (int &v: nums) {\n if (v < K) ++m[v];\n }\n\n int res = 0, hk = K / 2, ot;\n for(auto &[k, v]: m) {\n if (k > hk) break;\n ot = K - k;\n if (ot == k) {\n res += v / 2;\n break;\n }\n if (m[ot] == 0) continue; \n res += min(m[ot], v);\n }\n return res;\n }\n};",
"memory": "65798"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n std::sort(nums.begin(), nums.end());\n\n map<int, int> found;\n int count(0);\n\n for (int i = 0; i < nums.size() && nums[i] < k; ++i) {\n int other = k-nums[i];\n if (found[other] > 0) {\n ++count;\n --found[other];\n } else {\n found[nums[i]]++;\n }\n }\n return count;\n }\n};",
"memory": "65798"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); cout.tie(NULL);\n }\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int> mp;\n for (auto it : nums) if (it < k) mp[it]++;\n\n int cnt = 0;\n if ((k & 1) == 0){\n int key = k >> 1;\n if (mp.find(key) != mp.end())\n {\n int choosen = mp[key];\n cnt += (choosen >> 1);\n mp.erase(key);\n }\n }\n\n /*\n for (auto it : mp){\n cout << it.first << \" \" << it.second << \" \";\n //cout << k-it.first << \" \" << mp[k-it.first ] << \"\\n\";\n }*/\n for (auto it : mp)\n {\n int val = it.first;\n int c1 = it.second;\n if (c1 == 0) continue;\n if (mp.find(k-val) == mp.end()) continue;\n int c2 = mp[k-val];\n int chooseTime = min(c1, c2);\n cnt += chooseTime;\n mp[val] -= chooseTime;\n mp[k-val] -= chooseTime;\n }\n return cnt;\n }\n};",
"memory": "66014"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); cout.tie(NULL);\n }\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int> mp;\n for (auto it : nums) if (it < k) mp[it]++;\n\n int cnt = 0;\n if ((k & 1) == 0){\n int key = k >> 1;\n if (mp.find(key) != mp.end())\n {\n int choosen = mp[key];\n cnt += (choosen >> 1);\n mp.erase(key);\n }\n }\n\n for (auto it : mp)\n {\n int val = it.first;\n int c1 = it.second;\n if (c1 == 0) continue;\n int ch = k-val;\n if (mp.find(ch) == mp.end()) continue;\n int c2 = mp[ch];\n int chooseTime = min(c1, c2);\n cnt += chooseTime;\n mp[val] -= chooseTime;\n mp[k-val] -= chooseTime;\n }\n return cnt;\n }\n};",
"memory": "66014"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> diffs;\n int result=0;\n for (int i=0;i<nums.size();i++) {\n if(diffs.find(nums[i])!=diffs.end()) {\n result++;\n if (diffs[nums[i]]>1) {\n diffs[nums[i]]--;\n }\n else {\n diffs.erase(nums[i]);\n }\n }\n else if (nums[i]<k) {\n if (diffs.find(k-nums[i])==diffs.end()) {\n diffs[k-nums[i]]=1;\n }\n else {\n diffs[k-nums[i]]++;\n }\n }\n }\n\n return result;\n }\n};",
"memory": "66230"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(std::vector<int> &nums, int k)\n {\n std::vector<int> selected_nums;\n int min = INT_MAX;\n int max = 0;\n for (const int num : nums)\n {\n if (num < k)\n selected_nums.push_back(num);\n }\n std::sort(selected_nums.begin(), selected_nums.end());\n\n int left = 0;\n int right = selected_nums.size() - 1;\n int count_matching_sum = 0;\n while (left < right)\n {\n int sum_tmp = selected_nums.at(left) + selected_nums.at(right);\n if (sum_tmp == k)\n {\n count_matching_sum++;\n right--;\n left++;\n }\n else\n {\n int left_tmp = left + 1;\n while (left_tmp < right)\n {\n if (selected_nums.at(left_tmp) + selected_nums.at(right) == k)\n {\n left = left_tmp + 1;\n count_matching_sum++;\n break;\n }\n else\n {\n left_tmp++;\n }\n }\n right--;\n }\n }\n \n return count_matching_sum;\n }\n \n};",
"memory": "66230"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n vector<int> v;\n for(int i : nums) {\n if(i < k) v.push_back(i);\n }\n sort(v.begin(), v.end());\n \n int left = 0, right = v.size() - 1;\n int ans = 0;\n \n while(left < right) {\n int sum = v[left] + v[right];\n if(sum == k) {\n ans++;\n left++;\n right--;\n } else if(sum < k) {\n left++;\n } else {\n right--;\n }\n }\n \n return ans;\n }\n};",
"memory": "66446"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n if (nums.size() == 1) return 0;\n int ops = 0;\n int val = 0;\n unordered_map<int, int> m(nums.size()/2);\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] >= k) continue;\n val = k-nums[i];\n if (m[val] > 0) {\n m[val]--;\n ops++;\n } else {\n m[nums[i]]++;\n }\n }\n return ops;\n }\n};",
"memory": "66446"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n\n\n int len = nums.size();\n if(len==1)return 0;\n long long int count=0;\n unordered_map<long long int,long long int> hash;\n\n for(int i=0;i<len;i++){\n if(nums[i]>=k)continue;\n hash[nums[i]]++;\n }\n for(int i=0;i<len;i++){\n if(nums[i]>=k)continue;\n if(nums[i]*2 == k){\n count += min(hash[nums[i]],hash[k-nums[i]])/2;\n }else{\n count += min(hash[nums[i]],hash[k-nums[i]]);\n }\n\n hash[nums[i]]=0;\n hash[k-nums[i]]=0;\n }\n \n\n return count;\n }\n};",
"memory": "66663"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "static const auto speedup = []() {\nstd::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0;\n}();\n\nclass Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> frequency;\n for (int num : nums){\n if (num < k){\n frequency[num]++;\n frequency[k-num] += 0;\n }\n }\n\n unordered_set<int> seen;\n int numOperations = 0;\n\n for (const auto &[key,value] : frequency){\n int compliment = k - key;\n if (seen.find(compliment) != seen.end()){\n continue;\n }\n\n seen.insert(key);\n\n if (key == compliment){\n if (frequency[compliment] >= 2){\n numOperations += frequency[compliment] / 2;\n }\n }\n else if (frequency[key] >= 1 && frequency[compliment] >= 1){\n int m = min(frequency[key], frequency[compliment]);\n numOperations += m;\n } \n }\n return numOperations; \n }\n};",
"memory": "66879"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n\n int c=0;\n vector<int>v;\n int target=k;\n for(int i=0;i<nums.size();i++)\n {\n v.push_back(nums[i]);\n\n }\n \n sort(v.begin(),v.end());\n int p1=0,p2=v.size()-1;\n for(int i=0;i<v.size();i++)\n {\n if(v[p1]+v[p2]<target)\n {\n p1++;\n }\n else if(v[p1]+v[p2]>target)\n {\n p2--;\n }\n else if(v[p1]+v[p2]==target && p1<p2)\n {\n c++;\n p1++;\n p2--;\n }\n\n }\n return c;\n }\n};",
"memory": "67095"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n unordered_map<int, int> count; // num, number of occurences\n count.reserve(nums.size());\n int opp_count = 0;\n\n for (int& x : nums){\n if (x > k) continue;\n count[x]++;\n }\n\n for (int& x : nums){\n if (count.find(k-x) != count.end()){ // if k-x exists, possible pair\n if (k%2 == 0 && x == k/2){ // if x = k/2 then check if there is more than 1 to pair with\n int num_pairs = floor(count[x]/2); // could be 0 even if count[x] = 1\n opp_count += num_pairs;\n count[x] -= num_pairs * 2;\n } else {\n int num_pairs = min(count[x],count[k-x]); // could be 0 (no pairs)\n opp_count += num_pairs;\n count[x] -= num_pairs;\n count[k-x] -= num_pairs;\n } \n }\n }\n\n return opp_count;\n }\n};",
"memory": "68609"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n unordered_map<int, int> count; // num, number of occurences\n count.reserve(nums.size());\n int opp_count = 0;\n\n for (int& x : nums){\n if (x > k) continue;\n count[x]++;\n }\n\n for (int& x : nums){\n if (count.find(k-x) != count.end()){ // if k-x exists, possible pair\n if (k%2 == 0 && x == k/2){ // if x = k/2 then check if there is more than 1 to pair with\n int num_pairs = floor(count[x]/2); // could be 0 even if count[x] = 1\n opp_count += num_pairs;\n count[x] -= num_pairs * 2;\n } else {\n int num_pairs = min(count[x],count[k-x]); // could be 0 (no pairs)\n opp_count += num_pairs;\n count[x] -= num_pairs;\n count[k-x] -= num_pairs;\n } \n }\n }\n\n return opp_count;\n }\n};",
"memory": "68609"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n unordered_map<int, int> count; // num, number of occurences\n count.reserve(nums.size());\n int opp_count = 0;\n\n for (int& x : nums){\n if (x > k) continue;\n count[x]++;\n }\n\n for (auto x = count.begin(); x != count.end();){\n if (count.find(k-x->first) != count.end()){\n if (k%2 == 0 && x->first == k/2){\n int num_pairs = floor(x->second/2);\n opp_count += num_pairs;\n x->second -= num_pairs * 2;\n if (x->second < 2) {\n x = count.erase(x);\n continue;\n }\n } else {\n int num_pairs = min(x->second, count[k-x->first]);\n opp_count += num_pairs;\n x->second -= num_pairs;\n count[k-x->first] -= num_pairs;\n if (count[k-x->first] < 1) count.erase(k-x->first);\n if (x->second < 1){\n x = count.erase(x);\n continue;\n }\n }\n }\n x++;\n }\n\n // for (int& x : nums){\n // if (count.find(k-x) != count.end()){ // if k-x exists, possible pair\n // if (k%2 == 0 && x == k/2){ // if x = k/2 then check if there is more than 1 to pair with\n // int num_pairs = floor(count[x]/2); // could be 0 even if count[x] = 1\n // opp_count += num_pairs;\n // count[x] -= num_pairs * 2;\n // } else {\n // int num_pairs = min(count[x],count[k-x]); // could be 0 (no pairs)\n // opp_count += num_pairs;\n // count[x] -= num_pairs;\n // count[k-x] -= num_pairs;\n // } \n // }\n // }\n\n return opp_count;\n }\n};",
"memory": "68825"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n = nums.size();\n map<int,int>mp;\n int ans = 0;\n for(int i=0;i<n;i++){\n int rem = k - nums[i];\n if(mp.find(rem)!=mp.end() && mp[rem]>0){\n cout<<i<<endl;\n ans++;\n mp[rem]--;\n }\n else{\n mp[nums[i]]++;\n }\n }\n return ans;\n }\n};",
"memory": "69041"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int, int> M;\n int cnt = 0;\n for (int i = 0; i < nums.size(); i++) {\n int t = k - nums[i];\n if (M.find(t) != M.end() && M[t]) {\n M[t]--;\n cnt++;\n } else M[nums[i]]++;\n }\n return cnt;\n }\n};",
"memory": "69258"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int,int>m;\n int count=0;\n for(int i=0;i<nums.size();i++)\n {\n if(m.find(k-nums[i])!=m.end() && m[k-nums[i]]>0)\n {\n m[k-nums[i]]--;\n count++;\n }\n else m[nums[i]]++;\n }\n return count;\n }\n};",
"memory": "69474"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n map<int, int> m;\n \n // Store the final result\n int result = 0;\n \n // Iterate over the array nums[]\n for(auto i : nums)\n {\n \n // Decrement its frequency\n // in m and increment\n // the result by 1\n if (m.find(i) != m.end() && m[i] > 0)\n {\n m[i] = m[i] - 1;\n result++;\n }\n \n // Increment its frequency by 1\n // if it is already present in m.\n // Otherwise, set its frequency to 1\n else\n {\n m[k - i] = m[k - i] + 1;\n }\n }\n \n return result;\n \n \n }\n};",
"memory": "69474"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int, int> mp;\n int n= nums.size();\n int res=0;\n for (int i=0;i<n;i++){\n if (mp.find(nums[i])==mp.end() || mp[nums[i]]==0){\n if (mp.find(k-nums[i])==mp.end()){\n mp[k-nums[i]]=1;\n }else{\n mp[k-nums[i]]++;\n }\n \n }else{\n mp[nums[i]]--;\n res++;\n } \n }\n return res;\n }\n};\n/*\nmp= 4->0, 3->(), \n*/",
"memory": "69690"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n unordered_map<int,int> map;\n int count = 0;\n for(int i=0;i<nums.size();i++){\n if(map.find(nums[i]) != map.end()){\n count++;\n map[nums[i]]--;\n if(map[nums[i]]==0) map.erase(nums[i]);\n }\n else{\n map[k-nums[i]]++;\n }\n }\n return count;\n }\n};",
"memory": "69906"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n unordered_map<int,int> mp;\n int op = 0;\n for(int i=0;i<nums.size();i++){\n // if(nums[i] >= k)\n // break;\n if(mp.find(k - nums[i]) != mp.end()){\n op++;\n mp[k-nums[i]]--;\n if(mp[k-nums[i]] == 0){\n mp.erase(k-nums[i]);\n }\n }else{\n mp[nums[i]]++;\n }\n }\n return op;\n }\n};",
"memory": "70123"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n return solve(0, nums.size()-1, nums, k);\n }\n int solve(int i, int j, vector<int>& nums, int k){\n if(i >= j) return 0;\n \n int take = 0, reject = 0;\n if (nums[i] + nums[j] == k) take = 1 + solve(i+1, j-1, nums, k);\n if (nums[i] + nums[j] > k) reject = solve(i, j-1, nums, k);\n if (nums[i] + nums[j] < k) reject = solve(i+1, j, nums, k);\n \n return max(reject, take);\n }\n};",
"memory": "70339"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n map<int,int> mp;\n int ans = 0;\n for(int i =0; i< nums.size(); i++){\n if(mp.find(k-nums[i]) != mp.end() and mp[k-nums[i]] > 0){\n ans ++;\n mp[k-nums[i]]--;\n }else{\n mp[nums[i]]++;\n }\n }\n return ans;\n }\n};",
"memory": "70555"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int op = 0;\n unordered_map<int, pair<int, int>> hash;\n\n for(int i=0 ; i<nums.size() ; i++) {\n int diff = k - nums[i];\n if(hash.find(diff) != hash.end() and hash[diff].first != i and hash[diff].second != 0) {\n op++;\n hash[diff].second--;\n } else {\n hash[nums[i]].first = i;\n hash[nums[i]].second++;\n }\n }\n\n return op;\n }\n};",
"memory": "70771"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int maxOperations(vector<int>& nums, int k) {\n int ans = 0;\n unordered_map<int, int> count;\n\n for (const int num : nums)\n ++count[num];\n\n for (const auto& [num, freq] : count)\n if (const auto it = count.find(k - num); it != count.end())\n ans += min(freq, it->second);\n\n return ans / 2;\n }\n};",
"memory": "70988"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int> umap;\n int ans=0;\n\n for(int i=0;i<nums.size();i++){\n if(umap.find(nums[i])==umap.end()){\n umap[nums[i]]=0;\n }\n umap[nums[i]]++;\n }\n\n for(int i=0;i<nums.size();i++){\n int cur = nums[i];\n int diff = k-cur;\n\n if(umap.find(diff)!=umap.end()){\n if(cur == diff && umap[cur]>=2){\n ans+=1;\n umap[cur]-=2; \n } else if(cur!=diff && umap[cur]>=1 && umap[diff]>=1){\n ans+=1;\n umap[cur]-=1;\n umap[diff]-=1;\n }\n }\n } \n\n return ans;\n }\n};",
"memory": "71204"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int maxOperations(vector<int>& nums, int k) {\n int ans = 0;\n unordered_map<int, int> count;\n\n for (const int num : nums)\n ++count[num];\n\n for (const auto& [num, freq] : count)\n if (const auto it = count.find(k - num); it != count.end())\n ans += min(freq, it->second);\n\n return ans / 2;\n }\n};",
"memory": "71204"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int>store;\n for(auto c:nums)\n store[c]++;\n\n int count=0;\n for(auto c:nums)\n {\n if(store[c]<=0)\n continue;\n store[c]--;\n int remaining = k-c;\n if(remaining<0)\n continue;\n if(store.count(remaining) && store[remaining]>0)\n {\n count++;\n store[remaining]--;\n }\n }\n return count;\n }\n};",
"memory": "71420"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int maxOperations(vector<int>& nums, int k) {\n int ans = 0;\n unordered_map<int, int> count;\n\n for (const int num : nums)\n ++count[num];\n\n for (const auto& [num, freq] : count)\n if (const auto it = count.find(k - num); it != count.end())\n ans += min(freq, it->second);\n\n return ans / 2;\n }\n};",
"memory": "71420"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n=nums.size();\n int c=0;\n unordered_map<int,int> mp;\n int sum;\n for(int i=0;i<n;i++){\n sum = k-nums[i];\n if(mp.find(sum)!=mp.end()){\n // Found a pair whose sum =k;\n c++;\n mp[sum]--;\n if(mp[sum] ==0){\n mp.erase(sum);\n }\n } else {\n mp[nums[i]]++;\n }\n }\n return c;\n }\n};",
"memory": "71636"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map <int, int> hash; int count{0};\n for (int & i : nums){\n int comp = k - i;\n unordered_map <int, int> :: iterator itr = hash.find(comp);\n if (itr != hash.end()){\n count++;\n itr->second--;\n if (itr->second == 0) hash.erase(itr);\n }\n else hash[i]++;\n }\n return count;\n }\n};",
"memory": "71636"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int> cnt;\n int ans = 0;\n for (const int& num: nums) {\n int comp = k - num;\n if (!cnt.count(comp)) ++cnt[num];\n else {\n ++ans;\n --cnt[comp];\n if (!cnt[comp]) cnt.erase(comp);\n }\n }\n return ans;\n }\n};",
"memory": "71853"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int, int> counts;\n for (const auto& n : nums) {\n ++counts[n];\n }\n\n int res = 0;\n for (auto& val : counts) {\n if ((val.first > k) || (val.second <= 0)) {\n continue;\n }\n\n if (val.first * 2 == k) {\n res += floor(val.second / 2);\n } else if (auto found = counts.find(k - val.first); found != counts.end()) {\n res += min(val.second, found->second);\n val.second = -1;\n found->second = -1;\n }\n }\n\n return res;\n }\n};",
"memory": "71853"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nSolution()\n {\n ios_base::sync_with_stdio(false); \n cin.tie(NULL); \n cout.tie(NULL);\n }\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n int ans = 0;\n for (int e : nums){\n if (e > k) continue;\n auto it = map.find(k - e);\n if(it != map.end()){\n ++ans;\n --it->second;\n if (it->second==0)\n map.erase(it);\n }else{\n auto iit = map.insert({e, 1});\n if (!iit.second)\n ++iit.first->second;\n }\n }\n return ans;\n }\n};",
"memory": "73366"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int ans=0;\n unordered_multiset<int> hashSet;\n for (int i=0, n=nums.size(); i<n; i++) {\n if (nums[i]<k) {\n auto itr=hashSet.find(k-nums[i]);\n if (itr!=hashSet.end()) {\n hashSet.erase(itr);\n ans++;\n } else {\n hashSet.insert(nums[i]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "73366"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n size_t count = nums.size();\n unordered_multiset<int> remainingValues;\n int numOps = 0;\n for (int i = 0; i < count; ++i) {\n if (nums[i] >= k) continue;\n\n // Figure out the value's pair\n int curValuePair = k - nums[i];\n\n // Search for the pair in remaining values\n auto foundIt = remainingValues.find(curValuePair);\n if (foundIt != remainingValues.end()) {\n // If found, remove from set and count as an operation\n remainingValues.erase(foundIt);\n ++numOps;\n }\n else {\n // Otherwise add this value to the set\n remainingValues.insert(nums[i]);\n }\n }\n return numOps;\n }\n};",
"memory": "73583"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<ll,int> mp;\n int cnt=0,n=nums.size();\n\n for(auto it: nums)\n mp[it]++;\n\n for(auto it: mp)\n {\n if(mp.count(k-it.first) && mp[k-it.first]!=-1)\n {\n if(k%2==0 && it.first==k/2)\n cnt+=(mp[it.first]/2);\n else\n cnt+=min(it.second,mp[k-it.first]);\n \n mp[it.first]=-1;\n mp[k-it.first]=-1;\n }\n }\n\n return cnt;\n }\n};",
"memory": "73583"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int, int> occur; set<int> values;\n for (int n : nums) {occur[n]++; \n if (2*n <= k) values.insert(n);}\n int op = 0;\n for (auto it = values.begin(); it != values.end(); it++)\n {\n if (*it*2 == k) op += occur[*it]/2;\n else op += min(occur[*it], occur[k - *it]);\n }\n return op;\n }\n};",
"memory": "73799"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int result=0;\n // Sorting and Two pointers\n // int i=0,j=n-1;\n // while(i<j){\n // if(k<nums[i]+nums[j]) j--;\n // else if(k>nums[i]+nums[j]) i++;\n // else{\n // result++;\n // i++;\n // j--;\n // }\n // }\n // HashMap\n map<int,int>mpp;\n for(int i:nums){\n if(mpp.find(k-i)!=mpp.end()){\n result++;\n if(mpp[k-i]==1) mpp.erase(k-i);\n else mpp[k-i]--;\n }\n else\n mpp[i]++;\n }\n return result;\n }\n};",
"memory": "73799"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n = nums.size();\n int ans=0;\n vector<pair<int,int>>pqr;\n unordered_map<int,int>count;\n for(int i=0;i<n;i++)\n {\n if(nums[i]<k)\n{ count[nums[i]]++;\n pqr.push_back({nums[i],k-nums[i]});\n}\n }\n for(auto it:pqr)\n {\n\n if(count.find(it.second)!=count.end())\n {\n int a= it.first;\n int b = it.second;\n if(a!=b)\n {\n if(count[a]>0 && count[b]>0)\n {\n count[a]--;\n count[b]--;\n ans++;\n }\n }\n else\n {\n if(count[a]>=2)\n {\n count[a]=count[a]-2;\n ans++;\n }\n }\n\n \n }\n }\n return ans;\n }\n};",
"memory": "74015"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map <int, int> hash; int count{0};\n for (int & i : nums){\n int comp = k - i;\n map <int, int> :: iterator itr = hash.find(comp);\n if (itr != hash.end()){\n count++;\n itr->second--;\n if (itr->second == 0) hash.erase(itr);\n }\n else hash[i]++;\n }\n return count;\n }\n};",
"memory": "74015"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int ans = 0;\n std::map<int, int> CountOfEles;\n for(int num: nums)\n {\n int remain = k - num;\n if(CountOfEles.contains(remain))\n {\n CountOfEles[remain] -= 1;\n if(CountOfEles[remain] == 0)\n {\n CountOfEles.erase(remain);\n }\n ans += 1;\n }\n else\n {\n CountOfEles[num] += 1;\n }\n }\n return ans;\n }\n};",
"memory": "74231"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int,int>mp;\n int n=nums.size();\n int ans=0;\n for(int i=0;i<n;i++){\n if(mp.find(k-nums[i])!=mp.end()){\n mp[k-nums[i]]--;\n if(mp[k-nums[i]]==0){\n mp.erase(k-nums[i]);\n }\n ans++;\n }else{\n mp[nums[i]]++;\n }\n }\n return ans;\n }\n\n};",
"memory": "74231"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int,int> freq;\n long long sum = 0;\n int ans = 0;\n for(int i=0;i<nums.size();i++){\n int req = k - nums[i];\n if(freq.count(req)) {\n ans++;\n freq[req]--;\n if(freq[req]==0) freq.erase(req);\n }\n else freq[nums[i]]++;\n }\n return ans;\n }\n};",
"memory": "74448"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n sort(nums.begin() , nums.end());\n int n = nums.size() , res = 0;\n unordered_set<int> vis;\n unordered_map<int , int> num_counter;\n for(int& it : nums) num_counter[it]++;\n\n for(int& it : nums){\n int val = abs(k - it);\n if(it > val) break;\n if(vis.find(it) == vis.end()){\n if(num_counter.find(val) != num_counter.end()){\n if(it == val){\n if(num_counter[it] >= 2){\n res++;\n num_counter[it] -= 2;\n }\n else{\n vis.insert(it);\n num_counter.erase(it);\n }\n }\n else{\n num_counter[it]--;\n num_counter[val]--;\n res++;\n if(num_counter[it] == 0){\n vis.insert(it);\n num_counter.erase(it);\n }\n\n if(num_counter[val] == 0){\n vis.insert(val);\n num_counter.erase(val);\n }\n }\n }\n }\n }\n\n return res;\n }\n};",
"memory": "74448"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int>st;\n int count = 0;\n sort(nums.begin(),nums.end());\n for(auto x:nums){\n st[x]++;\n }\n for(int i = nums.size()-1;i >= 0;i--){\n int n = nums[i];\n if(st.count(n)){\n st[n]--;\n if(st[n] == 0)st.erase(n);\n if(st.count(k-n)){\n st[k-n]--;\n if(st[k-n] == 0)st.erase(k-n);\n count++;\n }else{\n st[n]++;\n }\n }\n }\n return count;\n }\n};",
"memory": "74664"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n \n int n = nums.size();\n\n unordered_map<int,int>mp;\n\n unordered_set<int>vis;\n\n for(int i=0;i<n;i++)\n {\n if(mp.find(nums[i])==mp.end())\n {\n mp[nums[i]] = 1;\n }\n else\n {\n mp[nums[i]] = mp[nums[i]] + 1;\n }\n }\n\n int ans=0;\n\n for(auto it:mp)\n {\n if(!vis.contains(it.first))\n {\n if(mp.find(k-it.first)!=mp.end())\n {\n if(it.first==k-it.first)\n {\n ans+=(mp[it.first]/2);\n }\n else\n {\n ans+=(min(mp[it.first],mp[k-it.first]));\n }\n\n vis.insert(it.first);\n vis.insert(k-it.first);\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "74664"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n unordered_map<int,bool>mp2;\n for(auto it : nums)\n mp[it]++;\n int ans = 0;\n for(const auto &it : mp)\n {\n int a = it.first;\n \n if(mp.find(k - a) != mp.end() && !mp2[a])\n { if(a == k - a)\n {ans += mp[a]/2;}\n else{\n ans += min(mp[a],mp[k-a]);\n }\n mp2[a] = true;\n mp2[k-a] = true; \n }\n }\n return ans;\n }\n};",
"memory": "74880"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> freq;\n unordered_map<int,bool> visit;\n for(auto ele:nums){\n freq[ele]++;\n }\n sort(nums.begin(),nums.end());\n int ans=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n int target=k-nums[i];\n if(target==nums[i]){\n ans+=(freq[target]/2);\n break;\n }\n if(target<nums[i]) break;\n if(visit[nums[i]]) continue;\n visit[nums[i]]=true;\n int s=i+1,e=n-1,mid;\n while(s<=e){\n mid=s+(e-s)/2;\n if(nums[mid]==target){\n ans+=min(freq[target],freq[nums[i]]);\n visit[target]=true;\n break;\n }\n else if(nums[mid]>target){\n e=mid-1;\n }\n else{\n s=mid+1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "74880"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int n = nums.size();\n int res = 0;\n std::map<int, int> sp;\n vector<int> num;\n vector<int> count;\n\n for(int i:nums)\n {\n sp[i]++;\n }\n\n for(auto& i:sp)\n {\n num.push_back(i.first);\n count.push_back(i.second);\n }\n\n int nn = num.size();\n for(int i = 0;i < nn;i++)\n {\n if(count[i] == 0)continue;\n if(k%2 == 0 && num[i] == k/2)\n {\n res += floor(count[i]/2.0);\n continue;\n }\n for(int j = i+1;j < nn;j++)\n {\n if(num[i] + num[j] == k)\n {\n res += min(count[i], count[j]);\n count[j] = 0;\n }\n }\n }\n return res;\n }\n};",
"memory": "75096"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int len = nums.size(), ans = 0;\n unordered_map<int, int> mp;\n for (int i: nums) {\n mp[i]++;\n }\n for (int i = 0; i < len && !mp.empty(); i++) {\n if (nums[i] == (k - nums[i])) {\n ans = ans + (mp[nums[i]]/ 2);\n mp.erase(nums[i]);\n } else {\n ans = ans + min(mp[nums[i]], mp[k - nums[i]]);\n mp.erase(nums[i]);\n mp.erase(k - nums[i]);\n }\n }\n return ans;\n }\n};",
"memory": "75096"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& v, int k) {\n int n = v.size();\n map<int,int> mp;\n for(auto it:v){\n mp[it]++;\n }\n vector<int> v1;\n for(auto it:mp){\n v1.push_back(it.first);\n }\n map<int,int> m1;\n for(auto it:v1){\n if(mp.find(k-it)!=mp.end()&&m1.find(k-it)==m1.end()){\n m1[it]++;\n }\n }\n int c=0;\n for(auto it:m1){\n if(it.first==k-it.first){\n c+=(mp[it.first])/2;\n }\n else{\n c+=min(mp[it.first],mp[k-it.first]);\n }\n }\n return c;\n }\n\n};",
"memory": "75313"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int, int>m;\n int cnt=0;\n for(auto x : nums) m[x]++;\n for(auto x:nums){\n int diff = k-x;\n if(diff==x){\n cnt += (m[diff]/2);\n m.erase(x);\n }\n else if(m.count(diff)){\n cnt += min(m[diff], m[x]);\n m.erase(x);\n m.erase(diff);\n }\n }\n return cnt;\n }\n};",
"memory": "75529"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n int ans = 0;\n unordered_map<int, int> counts;\n for (auto num : nums) {\n counts[num]++;\n }\n for (int num : nums) {\n int complement = k - num;\n if (complement == num) {\n ans += counts[num] / 2;\n counts.erase(num);\n } else if (counts.count(num) && counts.count(complement)) {\n ans += std::min(counts[num], counts[complement]);\n counts.erase(num);\n counts.erase(complement);\n }\n }\n return ans;\n \n }\n};",
"memory": "75745"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n // int n=nums.size();\n // sort(nums.begin(),nums.end());\n // int i=0,j=n-1,cnt=0;\n // while(i<j){\n // if((nums[i]+nums[j])==k) {\n // cnt++;\n // i++;\n // j--;\n // }\n // else if((nums[i]+nums[j]) >k){\n // j--;\n // }\n // else i++;\n // }\n // return cnt;\n\n\n //using map\n\n unordered_map<int,int> mp;\n int cnt=0;\n for(auto x:nums) mp[x]++;\n \n for(auto x:nums){\n int diff=k-x;\n\n if(diff==x){\n cnt+=(mp[diff]/2);\n mp.erase(x);\n\n }\n else if(mp.count(diff)){\n cnt+=min(mp[diff] , mp[x]);\n mp.erase(x);\n mp.erase(diff);\n }\n }\n\n return cnt;\n \n }\n};\n",
"memory": "75961"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> mp; \n for(int i=0;i<nums.size();i++){\n mp[nums[i]]++; \n }\n int ans=0;\n for(int i=0;i<nums.size();i++){\n if( 2*nums[i]!=k && mp[k-nums[i]]>0 && mp[nums[i]]>0){\n mp[k-nums[i]]--; \n mp[nums[i]]--; \n if(mp[k-nums[i]]==0){\n mp.erase(k-nums[i]); \n }\n if(mp[nums[i]]==0){\n mp.erase(nums[i]); \n }\n ans++; \n }else if(2*nums[i]==k && mp[k-nums[i]]>1 && mp[nums[i]]>1){\n mp[k-nums[i]]--; \n mp[nums[i]]--; \n if(mp[k-nums[i]]==0){\n mp.erase(k-nums[i]); \n }\n if(mp[nums[i]]==0){\n mp.erase(nums[i]); \n }\n ans++;\n }\n }\n return ans; \n }\n};",
"memory": "75961"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n std::unordered_map<int, int> m(nums.size());\n int total = 0;\n for (int num : nums) {\n if (0 != m[k-num]) {\n m[k-num]--;\n total++;\n } else {\n m[num]++;\n }\n }\n return total;\n }\n};",
"memory": "76178"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n unordered_map<int,int> mp; \n for(int i=0;i<nums.size();i++){\n mp[nums[i]]++; \n }\n int ans=0;\n for(int i=0;i<nums.size();i++){\n if( 2*nums[i]!=k && mp[k-nums[i]]>0 && mp[nums[i]]>0){\n mp[k-nums[i]]--; \n mp[nums[i]]--; \n if(mp[k-nums[i]]==0){\n mp.erase(k-nums[i]); \n }\n if(mp[nums[i]]==0){\n mp.erase(nums[i]); \n }\n ans++; \n }else if(2*nums[i]==k && mp[k-nums[i]]>1 && mp[nums[i]]>1){\n mp[k-nums[i]]--; \n mp[nums[i]]--; \n if(mp[k-nums[i]]==0){\n mp.erase(k-nums[i]); \n }\n if(mp[nums[i]]==0){\n mp.erase(nums[i]); \n }\n ans++;\n }\n }\n return ans; \n }\n};",
"memory": "76178"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int, int> Count;\n for (int& x : nums) {\n Count[x]++;\n }\n int ans = 0;\n for (int& x : nums) {\n if (Count[k-x] > 0 && Count[x] > 0) {\n if (k - x == x) {\n if (Count[x] > 1) {\n Count[x] -= 2;\n ans++;\n }\n } else {\n Count[k - x]--;\n Count[x]--;\n ans++;\n }\n }\n }\n return ans;\n }\n};",
"memory": "77691"
} |
1,798 | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxOperations(vector<int>& nums, int k) {\n map<int,int>myMap;\n for(int i:nums){\n myMap[i]++;\n }\n int count =0;\n for(int i:nums){\n if(myMap[i]==0) continue;\n int j = k-i;\n if(myMap[j]>0){\n if(i==j){\n if(myMap[j]>1){\n count++;\n myMap[j] -=2;\n }\n }else{\n count++;\n myMap[i]--;\n myMap[j]--; \n }\n \n }\n }\n return count;\n }\n};",
"memory": "77691"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "//n\n//1*4+2\n//6*4+3\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n long int res=0,hell=1e9+7,pw=1;\n for(int i=1;i<=n;i++){\n if(pw*2<=i)pw=pw*2;\n res=(res*(pw*2)+i)%hell;\n\n // cout<<res<<\" //\";\n }\n return res;\n }\n};",
"memory": "7400"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n long ans = 0, MOD = 1e9 + 7, len = 0;\n for (int i = 1; i <= n; ++i) {\n if(__builtin_popcount(i) == 1) ++len;\n ans = ((ans << len) % MOD + i) % MOD;\n }\n return ans;\n}\n};",
"memory": "7500"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n int MOD = 1e9 + 7;\n long long result = 0;\n int length = 0;\n\n for (int i = 1; i <= n; i++) {\n if ((i & (i - 1)) == 0) {\n length++;\n }\n\n result = ((result << length) % MOD + i) % MOD;\n }\n\n return result;\n \n }\n};",
"memory": "7600"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n int MOD = 1e9 + 7;\n long long result = 0;\n int length = 0;\n\n for (int i = 1; i <= n; i++) {\n if ((i & (i - 1)) == 0) {\n length++;\n }\n\n result = ((result << length) % MOD + i) % MOD;\n }\n\n return result;\n \n }\n};",
"memory": "7700"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n\n int mod = 1e9+7;\n int count=0;\n long long ans=0;\n int val=1;\n while(val<=n){\n count = log2(val)+1;\n ans = ((ans<<count) + val)%mod;\n val++;\n }\n return ans;\n }\n};",
"memory": "7800"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n using ll = long long;\n const ll mod = (ll)1E9 + 7;\n int count_len(int x) {\n for (int i = 30; i >= 1; i -= 1) {\n if (x & (1 << i)) return i + 1;\n }\n return 1;\n }\npublic:\n int concatenatedBinary(int n) {\n int len = 0;\n ll res = 0;\n for (int i = 1; i <= n; i += 1) {\n int cur_len = count_len(i);\n res = res * (1LL << cur_len) + i;\n res %= mod;\n }\n return (int)res;\n }\n};",
"memory": "7800"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n const int MOD = 1e9 + 7;\n long long result = 0;\n int length = 0;\n\n for (int i = 1; i <= n; ++i) {\n int num = i;\n int bits = 0;\n \n \n while (num > 0) {\n num >>= 1;\n ++bits;\n }\n\n \n result = ((result << bits) + i) % MOD;\n }\n int r=result;\n return r;\n }\n};\n",
"memory": "7900"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int concatenatedBinary( int n )\n {\n const int base = 1000 * 1000 * 1000 + 7;\n\n std::vector< int > binary_digits_reversed;\n\n int result = 0;\n for ( int i = 1 ; i <=n ; ++i )\n {\n binary_digits_reversed.clear();\n for ( int j = i; j > 0; j >>= 1 )\n binary_digits_reversed.push_back( j & 0x01 );\n for ( auto rit = binary_digits_reversed.rbegin();\n rit != binary_digits_reversed.rend();\n ++rit )\n result = ( result * 2 + *rit ) % base;\n }\n\n return result;\n }\n};\n",
"memory": "8000"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int concatenatedBinary( int n )\n {\n const int base = 1000 * 1000 * 1000 + 7;\n\n std::vector< int > binary_digits_reversed;\n\n int result = 0;\n for ( int i = 1 ; i <=n ; ++i )\n {\n binary_digits_reversed.clear();\n for ( int j = i; j > 0; j >>= 1 )\n binary_digits_reversed.push_back( j & 0x01 );\n for ( auto rit = binary_digits_reversed.rbegin();\n rit != binary_digits_reversed.rend();\n ++rit )\n result = ( result * 2 + *rit ) % base;\n }\n\n return result;\n }\n};\n",
"memory": "8000"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "int memo[100001];\nauto init = [](){\n memo[1] = 1;\n for(int i = 2; i < 100001; i++){\n int x = log2(i);\n for(int j = x; j >= 0; j--){\n if(i & (1 << j)){\n int num = memo[i - 1];\n for(int k = j; k > -1; k--){\n num *= 2;\n num %= 1000000007;\n num += (((1 << k) & i) != 0);\n }\n memo[i] = num;\n break;\n }\n }\n }\n return 0;\n}();\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n return memo[n];\n }\n};",
"memory": "8100"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "int memo[100001];\nauto init = [](){\n memo[1] = 1;\n for(int i = 2; i < 100001; i++){\n for(int j = 30; j >= 0; j--){\n if(i & (1 << j)){\n int num = memo[i - 1];\n for(int k = j; k > -1; k--){\n num *= 2;\n num %= 1000000007;\n num += (((1 << k) & i) != 0);\n }\n memo[i] = num;\n break;\n }\n }\n }\n return 0;\n}();\nclass Solution {\npublic:\n int concatenatedBinary(int n) {\n return memo[n];\n }\n};",
"memory": "8200"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n constexpr static const int Base = 1'000'000'007;\n static inline int pow2(int shifts) {\n static vector<int> cache{1, 2, 4, 8, 16, 32, 64, 128, 256, 512};\n while (shifts >= cache.size()) {\n cache.push_back(cache.back() * 2 % Base);\n }\n return cache[shifts];\n }\n\npublic:\n int concatenatedBinary(int n) {\n long long x = 1;\n for (int i = 2; i <= n; ++i) {\n // count the leading zeros in binary form of i\n int lz = __builtin_clz(i);\n // the previous number has to shift to the left by (32-lz) bits\n int shifts = 32 - lz;\n x = (x * pow2(shifts) % Base + i) % Base;\n }\n return x;\n }\n};",
"memory": "8800"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n long mod = 1e9 + 7;\n\n long multiply(long a,long b){\n a%= mod;b%= mod;\n return (a*b)%mod;\n }\n\n void inverseModulo(long a,long m,long *x,long *y){ //ax congruent to 1 mod m\n\n if(!a){\n *x=0;\n *y=1;\n return ;\n }\n long x1,y1;\n inverseModulo(m%a,a,&x1,&y1);\n *x=y1-(m/a)*x1;\n *y=x1;\n return;\n }\n\n long moduloDivision(long a,long b,long m){ // (a*(returnValue))mod m congruent to b mod m\n //https://www.geeksforgeeks.org/modular-division/ and https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/\n long x,y;\n inverseModulo(b, m, &x, &y);\n x%=m;\n return (x*a)%m;\n }\n\n long power(long n,long r){ //calculates (n^r)%mod in logarithmic time\n if(r==0) return 1;\n if(r==1) return n;\n if(r%2){\n auto tmp=power(n, (r-1)/2);\n return multiply(multiply(n,tmp),tmp);\n }\n auto tmp=power(n, r/2);\n return multiply(tmp, tmp);\n }\n\n long sumOfAGPSeries(long a,long d,long b,long r,long n){\n if(r==1) return multiply(n, multiply(a, 2)+multiply(n-1, d))/2;\n long left=multiply(multiply(d, r), power(r,n-1)-1);\n left=a+moduloDivision(left,r-1,mod);\n left=multiply(left, b);\n left%=mod;\n long right=multiply(multiply(b, power(r, n)), a+multiply(n-1, d));\n long ans=right-left;\n ans=(ans%mod + mod) % mod;\n return moduloDivision(ans,r-1,mod);\n }\n\n int concatenatedBinary(int N) {\n long ans = 0;\n long bitCountOfN = log2(N) + 1;\n long nearestPowerOfTwo = pow(2, bitCountOfN - 1);\n long startOfGP = 0;\n while (nearestPowerOfTwo) { // iterating over each arithmetico–geometric sequence\n long a, d, b, r, n;\n a = N;\n d = -1;\n b = power(2, startOfGP);\n r = power(2, bitCountOfN);\n n = N - nearestPowerOfTwo + 1;\n ans += sumOfAGPSeries(a, d, b, r, n);\n ans %= mod;\n startOfGP += n * bitCountOfN;\n N = nearestPowerOfTwo - 1;\n nearestPowerOfTwo >>= 1;\n bitCountOfN--;\n }\n\n return ans;\n }\n \n};",
"memory": "8900"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n long mod = 1e9 + 7;\n\n long multiply(long a,long b){\n a%= mod;b%= mod;\n return (a*b)%mod;\n }\n\n void inverseModulo(long a,long m,long *x,long *y){ //ax congruent to 1 mod m\n\n if(!a){\n *x=0;\n *y=1;\n return ;\n }\n long x1,y1;\n inverseModulo(m%a,a,&x1,&y1);\n *x=y1-(m/a)*x1;\n *y=x1;\n return;\n }\n\n long moduloDivision(long a,long b,long m){ // (a*(returnValue))mod m congruent to b mod m\n //https://www.geeksforgeeks.org/modular-division/ and https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/\n long x,y;\n inverseModulo(b, m, &x, &y);\n x%=m;\n return (x*a)%m;\n }\n\n long power(long n,long r){ //calculates (n^r)%mod in logarithmic time\n if(r==0) return 1;\n if(r==1) return n;\n if(r%2){\n auto tmp=power(n, (r-1)/2);\n return multiply(multiply(n,tmp),tmp);\n }\n auto tmp=power(n, r/2);\n return multiply(tmp, tmp);\n }\n\n long sumOfAGPSeries(long a,long d,long b,long r,long n){\n if(r==1) return multiply(n, multiply(a, 2)+multiply(n-1, d))/2;\n long left=multiply(multiply(d, r), power(r,n-1)-1);\n left=a+moduloDivision(left,r-1,mod);\n left=multiply(left, b);\n left%=mod;\n long right=multiply(multiply(b, power(r, n)), a+multiply(n-1, d));\n long ans=right-left;\n ans=(ans%mod + mod) % mod;\n return moduloDivision(ans,r-1,mod);\n }\n\n int concatenatedBinary(int N) {\n long ans = 0;\n long bitCountOfN = log2(N) + 1;\n long nearestPowerOfTwo = pow(2, bitCountOfN - 1);\n long startOfGP = 0;\n while (nearestPowerOfTwo) { // iterating over each arithmetico–geometric sequence\n long a, d, b, r, n;\n a = N;\n d = -1;\n b = power(2, startOfGP);\n r = power(2, bitCountOfN);\n n = N - nearestPowerOfTwo + 1;\n ans += sumOfAGPSeries(a, d, b, r, n);\n ans %= mod;\n startOfGP += n * bitCountOfN;\n N = nearestPowerOfTwo - 1;\n nearestPowerOfTwo >>= 1;\n bitCountOfN--;\n }\n\n return ans;\n }\n \n};",
"memory": "9000"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n int mod = 1000000007;\n int ans = 0;\n stack<int> st;\n\n for (int i = 1; i <= n; i++) {\n int j = i;\n while (j > 0) {\n st.push(j%2);\n j /= 2;\n }\n while (!st.empty()) {\n ans = 2*ans + st.top();\n st.pop();\n ans %= mod;\n }\n }\n\n return ans;\n }\n};",
"memory": "9300"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "static const int mod = 1e9+7;\nint count(int a){\n int ret = 0;\n while(a){\n ret++;\n a >>= 1;\n }\n return ret;\n}\nclass Solution {\npublic:\n long long f(int n){\n if(n == 1) return 1;\n int bit = count(n);\n long long ans = (f(n-1) << bit) + n;\n ans %= mod;\n return ans;\n }\n int concatenatedBinary(int n) {\n return f(n);\n }\n};",
"memory": "10600"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int concatenatedBinary(int n) {\n if (n == 1) return 1;\n auto p = concatenatedBinary(n - 1);\n int digit = log2(n) + 1;\n return ((int64_t)p * (1 << digit) + n) % ((int)1e9 + 7);\n }\n};",
"memory": "10900"
} |
1,800 | <p>Given an integer <code>n</code>, return <em>the <strong>decimal value</strong> of the binary string formed by concatenating the binary representations of </em><code>1</code><em> to </em><code>n</code><em> in order, <strong>modulo </strong></em><code>10<sup>9 </sup>+ 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>"1" in binary corresponds to the decimal value 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 27
<strong>Explanation: </strong>In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 505379714
<strong>Explanation</strong>: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10<sup>9</sup> + 7, the result is 505379714.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static const int mod = 1e9 + 7;\n\n int concatenatedBinary(int n) {\n if (n == 1)\n return 1;\n long prev = concatenatedBinary(n - 1);\n prev = prev << ((int)floor(log2(n)) + 1);\n prev += n;\n prev %= mod;\n return prev;\n }\n};",
"memory": "11100"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n #pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\n#include <limits>\n\nauto init = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ofstream out(\"user.out\");\n int num;\n do {\n std::cin.clear(); // Clear any error flags\n std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n'); \n std::cin >> num;\n if (std::cin.fail()) continue;\n if (num == -1) \n out << \"no cycle\\n\";\n else\n out << \"tail connects to node index \" << num << \"\\n\";\n } while (!std::cin.eof());\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *detectCycle(ListNode *head) {\n return head;\n }\n};",
"memory": "7300"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n #pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\n#include <limits>\n\nauto init = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ofstream out(\"user.out\");\n int num;\n do {\n std::cin.clear(); // Clear any error flags\n std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n'); \n std::cin >> num;\n if (std::cin.fail()) continue;\n if (num == -1) \n out << \"no cycle\\n\";\n else\n out << \"tail connects to node index \" << num << \"\\n\";\n } while (!std::cin.eof());\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *detectCycle(ListNode *head) {\n return head;\n }\n};",
"memory": "7400"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.