3943. Number of Pairs After Increment
Description
You are given two integer arrays nums1 and nums2, and a 2D integer array queries.
Each queries[i] is one of the following types:
[1, x, y, val]β Addvalto every element innums2[x..y].[2, tot]β Compute the number of pairs(j, k)such thatnums1[j] + nums2[k] == tot.
Return an integer array answer, where answer[j] is the number of pairs for the jth query of type 2.
Β
Example 1:
Input: nums1 = [1,2], nums2 = [3,4], queries = [[2,5],[1,0,0,2],[2,5]]
Output: [2,1]
Explanation:
queries[0] = [2, 5]: Valid pairs arenums1[0] + nums2[1] = 1 + 4 = 5andnums1[1] + nums2[0] = 2 + 3 = 5.queries[1] = [1, 0, 0, 2]: Add 2 tonums2[0], resulting innums2 = [5, 4].queries[2] = [2, 5]: Valid pair isnums1[0] + nums2[1] = 1 + 4 = 5.- Thus, the
answer = [2, 1].
Example 2:
Input: nums1 = [1,1], nums2 = [2,2,3], queries = [[2,4],[1,0,1,1],[2,4]]
Output: [2,6]
Explanation:
queries[0] = [2, 4]: Valid pairs arenums1[0] + nums2[2] = 1 + 3andnums1[1] + nums2[2] = 1 + 3.queries[1] = [1, 0, 1, 1]: Add 1 tonums2[0]andnums2[1], resulting innums2 = [3, 3, 3].queries[2] = [2, 4]: Every element ofnums1 = [1, 1]pairs with every element ofnums2 = [3, 3, 3]as1 + 3 = 4. That gives2 Γ 3 = 6pairs in total.- Thus, the
answer = [2, 6].
Example 3:
Input: nums1 = [2,5,8,4], nums2 = [1,3,8], queries = [[2,9],[1,1,2,1],[2,10]]
Output: [1,0]
Explanation:
queries[0] = [2, 9]: Only valid pair isnums1[2] + nums2[0] = 8 + 1 = 9.queries[1] = [1, 1, 2, 1]: Add 1 tonums2[1]andnums2[2], resulting inβββββββnums2 = [1, 4, 9].queries[2] = [2, 10]: No pair sums to10.- Thus, the
answer = [1, 0].
Β
Constraints:
1 <= nums1.length <= 51 <= nums2.length <= 5 * 1041 <= nums1[i], nums2[i] <= 1051 <= queries.length <= 5 * 104queries[i].length == 2 or 4queries[i] == [1, x, y, val], orqueries[i] == [2, tot]0 <= x <= y < nums2.length1 <= val <= 1051 <= tot <= 109βββββββ
Solutions
Solution 1
1 | |
1 | |
1 | |
1 | |