🧠
Hash Map Frequency Counting (Optimal)
💡 Using a hash map to track frequencies of complements lets us find pairs in a single pass, achieving linear time.
Intuition
For each number, check if its complement (k - num) exists in the map with positive count; if yes, form a pair and decrement frequency; else add current number to map.
Algorithm
- Initialize a frequency map (hash map) to count occurrences of numbers.
- Initialize count to zero.
- Iterate over each number in nums:
- - Calculate complement = k - num.
- - If complement exists in map with count > 0, decrement complement count and increment count.
- - Else, increment count of current number in map.
- Return count.
💡 This approach tracks what complements are needed and pairs them immediately, avoiding sorting and nested loops.
def maxOperations(nums, k):
freq = {}
count = 0
for num in nums:
complement = k - num
if freq.get(complement, 0) > 0:
count += 1
freq[complement] -= 1
else:
freq[num] = freq.get(num, 0) + 1
return count
# Driver code
if __name__ == '__main__':
print(maxOperations([1,2,3,4], 5)) # 2
print(maxOperations([3,1,3,4,3], 6)) # 1
Line Notes
freq = {}Initialize frequency map to track needed complements
for num in nums:Iterate over each number in array
complement = k - numCalculate complement needed to form pair
if freq.get(complement, 0) > 0Check if complement is available to pair
count += 1Increment count when pair is formed
freq[complement] -= 1Use one occurrence of complement
else: freq[num] = freq.get(num, 0) + 1Add current number to map if no complement found
import java.util.*;
public class Solution {
public static int maxOperations(int[] nums, int k) {
Map<Integer, Integer> freq = new HashMap<>();
int count = 0;
for (int num : nums) {
int complement = k - num;
if (freq.getOrDefault(complement, 0) > 0) {
count++;
freq.put(complement, freq.get(complement) - 1);
} else {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
}
return count;
}
public static void main(String[] args) {
System.out.println(maxOperations(new int[]{1,2,3,4}, 5)); // 2
System.out.println(maxOperations(new int[]{3,1,3,4,3}, 6)); // 1
}
}
Line Notes
Map<Integer, Integer> freq = new HashMap<>();Frequency map to track needed complements
for (int num : nums)Iterate over each number
int complement = k - num;Calculate complement needed
if (freq.getOrDefault(complement, 0) > 0)Check if complement available
count++;Increment count for formed pair
freq.put(complement, freq.get(complement) - 1);Use one complement occurrence
freq.put(num, freq.getOrDefault(num, 0) + 1);Add current number if no complement
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int maxOperations(vector<int>& nums, int k) {
unordered_map<int, int> freq;
int count = 0;
for (int num : nums) {
int complement = k - num;
if (freq[complement] > 0) {
count++;
freq[complement]--;
} else {
freq[num]++;
}
}
return count;
}
int main() {
vector<int> nums1 = {1,2,3,4};
cout << maxOperations(nums1, 5) << "\n"; // 2
vector<int> nums2 = {3,1,3,4,3};
cout << maxOperations(nums2, 6) << "\n"; // 1
return 0;
}
Line Notes
unordered_map<int, int> freq;Hash map to track needed complements
for (int num : nums)Iterate over each number
int complement = k - num;Calculate complement
if (freq[complement] > 0)Check if complement available
count++;Increment count for pair
freq[complement]--;Use one complement occurrence
freq[num]++;Add current number if no complement
function maxOperations(nums, k) {
const freq = new Map();
let count = 0;
for (const num of nums) {
const complement = k - num;
if (freq.get(complement) > 0) {
count++;
freq.set(complement, freq.get(complement) - 1);
} else {
freq.set(num, (freq.get(num) || 0) + 1);
}
}
return count;
}
// Test cases
console.log(maxOperations([1,2,3,4], 5)); // 2
console.log(maxOperations([3,1,3,4,3], 6)); // 1
Line Notes
const freq = new Map();Map to track needed complements
for (const num of nums)Iterate over each number
const complement = k - num;Calculate complement
if (freq.get(complement) > 0)Check if complement available
count++;Increment count for pair
freq.set(complement, freq.get(complement) - 1);Use one complement occurrence
freq.set(num, (freq.get(num) || 0) + 1);Add current number if no complement
Single pass over array with O(1) average map operations, total O(n). Space for map up to O(n).
💡 For n=100000, this means about 100,000 operations, very efficient for interviews.
Interview Verdict: Accepted
This is the best approach for large inputs and is commonly expected in interviews.