To improve the time complexity of your solution, you can avoid the nested loops that run in ( O(n \times m) ) time for each count query. Instead, you can preprocess vector b to create a frequency map of its elements. This way, you can answer each count query in ( O(n) ) time by iterating over vector a and using the frequency map to find the count of complementary values in b.
Here's how you can implement this approach without using the standard library's unordered maps (std::unordered_map), by creating your own simple hash map:
#include <vector>
using namespace std;
vector<int> solution(vector<int> a, vector<int> b, vector<vector<int>> queries) {
vector<int> results;
// Determine the range of elements in b to create an appropriate hash map size
int min_b = b[0], max_b = b[0];
for (int i = 1; i < b.size(); ++i) {
if (b[i] < min_b) min_b = b[i];
if (b[i] > max_b) max_b = b[i];
}
int size = max_b - min_b + 1;
// Create a frequency map for b using an array
vector<int> freq_b(size, 0);
for (int i = 0; i < b.size(); ++i) {
freq_b[b[i] - min_b]++;
}
for (int q = 0; q < queries.size(); ++q) {
vector<int>& query = queries[q];
if (query[0] == 0) {
// Update query: modify element in vector a
int idx = query[1];
int val = query[2];
a[idx] = val;
} else {
// Count query: count the number of pairs where a[i] + b[j] == x
int x = query[1];
int count = 0;
for (int i = 0; i < a.size(); ++i) {
int y = x - a[i];
// Check if y is within the range of b's elements
if (y >= min_b && y <= max_b) {
count += freq_b[y - min_b];
}
}
results.push_back(count);
}
}
return results;
}
Explanation:
-
Preprocessing b:
- Find the minimum and maximum values in
b to determine the range of possible values.
- Create a frequency array
freq_b of size equal to the range of elements in b.
- Populate
freq_b by counting the occurrences of each element in b.
-
Processing Queries:
- Update Query (
query[0] == 0): Update the specified element in vector a.
- Count Query: For each element in
a, calculate the complement y = x - a[i], and use the frequency map to find how many times y occurs in b.
- If
y is within the range of b's elements, add freq_b[y - min_b] to the count.
- This reduces the complexity of each count query to ( O(n) ).
Benefits of This Approach:
- Improved Time Complexity: Reduces the time complexity of count queries from ( O(n \times m) ) to ( O(n) ).
- No Standard Library Hash Maps: Avoids using
std::unordered_map by implementing a simple frequency map with an array.
- Efficient for Small Ranges: Works well when the range of elements in
b is not excessively large.
Notes:
- Ensure that the elements in
b do not have an excessively large range; otherwise, the frequency array may become too large to handle efficiently.
- If the range is too large, you may need to implement a custom hash map with collision handling (e.g., using open addressing or separate chaining).
By preprocessing b and using a frequency map, you avoid the nested loops and significantly improve the performance of your solution for large inputs.