Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
How many index pairs i < j satisfy 3 divides a[i] + a[j] for a = [1, 2, 3, 4, 5, 6, 7]?
- A. 8
- B. 11
- C. 6
- D. 7
Correct answer: D. 7
Bucket values by residue mod 3; a pair sums to a multiple of 3 when the residues are (0,0), (1,2) or (2,1). Counting by residues avoids the O(n^2) double loop, though the loop gives the same total.
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 ordered pairs (x, y) with 1 <= x, y <= 10 satisfy gcd(x, y) = 1?
- How many elements of [12, 19, 40, 7, 31, 22, 13, 8] have a decimal digit sum divisible by 4?
- How many perfect squares lie in the range [50, 250] inclusive?
- How many contiguous subarrays of [6, 10, 15, 4] have gcd exactly 1?
- A linked list holds 1..8 in order. Reverse the first group of 2 nodes, keep the next group of 2 as is, reverse the next, and so on. What…
- Exactly how many permutations of 1..5 can be sorted by a single stack (push and pop operations, output must be 1, 2, 3, 4, 5)?