Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B
Let A = [1, 4, 6, 9], B = [2, 3, 7] and S = [5, 9, 10, 13]. Count the ordered pairs (x from A, y from B) whose sum x + y lies in S.
- A. 4
- B. 3
- C. 2
- D. 9
Correct answer: C. 2
For each x in A, look up each y in B in a hash set S (or sort and use two pointers). Each pair is counted once because the first list supplies x and the second supplies y. Swapping roles counts a different set of ordered pairs.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many unordered pairs of positions in [5, 1, 4, 7, 2, 6] have bitwise XOR at most 3?
- What is the minimum number of characters you must DELETE from "aabbbccddddeee" so that every letter that remains appears the same number…
- A rod of length 17 must be cut into pieces each of length at most 5. What is the minimum number of cuts?
- What is the maximum number of DISJOINT non-empty contiguous subarrays of [1, -1, 2, -2, 3, 4, -7, 5, -5, 6] that each have sum 0?
- What is the minimum possible length of a single CONTIGUOUS block of [1, 3, 7, 4, 2, 5, 9] that you can delete (the rest is concatenated in…
- The sorted numbers [1, 3, 6, 7, 10, 11, 15] must be split into the fewest CONTIGUOUS groups so that in each group the largest minus the…