Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B
How many index pairs (i < j) in [4, 9, 1, 7, 12, 5, 8, 3] satisfy a[i] − a[j] = 3 (exactly 3, in this order)?
- A. 3
- B. 4
- C. 1
- D. 0
Correct answer: C. 1
Use a frequency map and, for each j, look up a[j] + d among earlier values (the earlier element is larger by exactly d). Counting pairs with a[j] − a[i] = d, the reverse order, gives a different count.
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
- Starting from the number 1, each operation either doubles the current number or adds 1. What is the minimum number of operations needed to…
- How many pairs (i, j) with 1 ≤ i ≤ j ≤ 12 have lcm(i, j) ≤ 12?
- Coins of values 1, 4, 9 and 16 are available in unlimited supply. What is the minimum number of coins that makes exactly 27?
- What is the maximum sum of a contiguous subarray of [4, -3, 5, 2, -7, 6, 1, 3] whose length is ODD?
- How many contiguous subarrays of [2, 3, 5, 4, 7, 6, 9] contain EXACTLY two odd numbers?
- Among the windows of exactly 3 consecutive elements of [5, 2, 9, 1, 7, 3, 8, 4], what is the smallest window sum that is at least 15?