Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2341.html
2341. Maximum Number of Pairs in Array
- Difficulty: Easy.
- Related Topics: Array, Hash Table, Counting.
- Similar Questions: Sort Characters By Frequency, Top K Frequent Words, Sort Array by Increasing Frequency.
Problem
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
Solution (Java, C++, Python)
-
class Solution { public int[] numberOfPairs(int[] nums) { Arrays.sort(nums); int pairs = 0; for (int i = 0; i < nums.length; i++) { if (i > 0 && nums[i] == nums[i - 1]) { nums[i] = -1; nums[i - 1] = -1; pairs++; } } return new int[] {pairs, nums.length - (2 * pairs)}; } } ############ class Solution { public int[] numberOfPairs(int[] nums) { int[] cnt = new int[101]; for (int x : nums) { ++cnt[x]; } int s = 0; for (int v : cnt) { s += v / 2; } return new int[] {s, nums.length - s * 2}; } }
-
class Solution: def numberOfPairs(self, nums: List[int]) -> List[int]: cnt = Counter(nums) s = sum(v // 2 for v in cnt.values()) return [s, len(nums) - s * 2] ############ # 2341. Maximum Number of Pairs in Array # https://leetcode.com/problems/maximum-number-of-pairs-in-array/ class Solution: def numberOfPairs(self, nums: List[int]) -> List[int]: n = len(nums) removed = 0 for x in Counter(nums).values(): removed += x // 2 return [removed, n - removed * 2]
-
class Solution { public: vector<int> numberOfPairs(vector<int>& nums) { vector<int> cnt(101); for (int& x : nums) { ++cnt[x]; } int s = 0; for (int& v : cnt) { s += v >> 1; } return {s, (int) nums.size() - s * 2}; } };
-
func numberOfPairs(nums []int) []int { cnt := [101]int{} for _, x := range nums { cnt[x]++ } s := 0 for _, v := range cnt { s += v / 2 } return []int{s, len(nums) - s*2} }
-
function numberOfPairs(nums: number[]): number[] { const n = nums.length; const count = new Array(101).fill(0); for (const num of nums) { count[num]++; } const sum = count.reduce((r, v) => r + (v >> 1), 0); return [sum, n - sum * 2]; }
-
/** * @param {number[]} nums * @return {number[]} */ var numberOfPairs = function (nums) { const cnt = new Array(101).fill(0); for (const x of nums) { ++cnt[x]; } const s = cnt.reduce((a, b) => a + (b >> 1), 0); return [s, nums.length - s * 2]; };
-
public class Solution { public int[] NumberOfPairs(int[] nums) { int[] cnt = new int[101]; foreach(int x in nums) { ++cnt[x]; } int s = 0; foreach(int v in cnt) { s += v / 2; } return new int[] {s, nums.Length - s * 2}; } }
-
impl Solution { pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> { let n = nums.len(); let mut count = [0; 101]; for &v in nums.iter() { count[v as usize] += 1; } let mut sum = 0; for v in count.iter() { sum += v >> 1; } vec![sum as i32, (n - sum * 2) as i32] } }
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).