Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
1.2 kB
6
Indexable
Never
    int twoSum(vector<int>& nums, int target, int m, int n) {
        unordered_map<int, int> indexMap;

        int count = 0;

        for (int i=0; i<nums.size(); i++) {
            indexMap[nums[i]] = i;
        }

        for (int i=0; i<nums.size(); i++) {
            int diff = target - nums[i];

            if (indexMap.find(diff) != indexMap.end() && indexMap[diff] != i) {
                if (i<m && indexMap[diff]>=m)
                    count++;
            }
        }

        return count;
    }

    vector<int> solve(vector<int>& a, vector<int>& b, vector<vector<int>>& queries) {

        int m = a.size();
        int n = b.size();

        vector<int> combined;

        vector<int> result;
        for(auto x:a)
            combined.push_back(x);

        for(auto x:b)
            combined.push_back(x);

        for (auto query: queries) {
            if (query[0] == 0) {
                combined[m+query[1]]+=query[2];
            }
            else if (query[0] == 1) { 
                vector<int> temp = twoSum(combined,query[1],m,n);
                result.push_back(temp);
            }
        }

        return result;

    }

    
Leave a Comment