2341. Maximum Number of Pairs in Array LeetCode Solution
In this guide, you will get 2341. Maximum Number of Pairs in Array LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Pairs in Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Number of Pairs in Array solution in C++
- Maximum Number of Pairs in Array solution in Java
- Maximum Number of Pairs in Array solution in Python
- Additional Resources
Problem Statement of Maximum Number of Pairs in Array
You are given a 0-indexed integer array nums. In one operation, you may do the following:
Choose two integers in nums that are equal.
Remove both integers from nums, forming a pair.
The operation is done on nums as many times as possible.
Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.
Example 1:
Input: nums = [1,3,2,1,3,2,2]
Output: [3,1]
Explanation:
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
Example 2:
Input: nums = [1,1]
Output: [1,0]
Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
Example 3:
Input: nums = [0]
Output: [0,1]
Explanation: No pairs can be formed, and there is 1 number leftover in nums.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2341. Maximum Number of Pairs in Array LeetCode Solution in C++
class Solution {
public:
vector<int> numberOfPairs(vector<int>& nums) {
constexpr int kMax = 100;
vector<int> ans(2);
vector<int> count(kMax + 1);
for (const int num : nums)
++count[num];
for (int i = 0; i <= kMax; ++i) {
ans[0] += count[i] / 2;
ans[1] += count[i] & 1;
}
return ans;
}
};
/* code provided by PROGIEZ */
2341. Maximum Number of Pairs in Array LeetCode Solution in Java
class Solution {
public int[] numberOfPairs(int[] nums) {
final int kMax = 100;
int[] ans = new int[2];
int[] count = new int[kMax + 1];
for (final int num : nums)
++count[num];
for (int i = 0; i <= kMax; ++i) {
ans[0] += count[i] / 2;
if (count[i] % 2 == 1)
++ans[1];
}
return ans;
}
}
// code provided by PROGIEZ
2341. Maximum Number of Pairs in Array LeetCode Solution in Python
class Solution:
def numberOfPairs(self, nums: list[int]) -> list[int]:
ans = [0] * 2
count = collections.Counter(nums)
for i in range(101):
ans[0] += count[i] // 2
ans[1] += count[i] & 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.