Data Structures & AlgorithmsHardGATE CS · competitive programming
How many pairs i < j in [2, 7, 3, 5, 8, 4] have A[i] + A[j] divisible by 3?
- A. 6
- B. 5
- C. 7
- D. 9
Correct answer: A. 6
Taking residues mod 3 gives 2, 1, 0, 2, 2, 1. The only zero-sum combinations are a residue 1 with a residue 2: 2 × 3 = 6 pairs, namely (2,7), (2,4), (7,5), (7,8), (5,4) and (8,4).
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 non-empty contiguous subarrays of [4, 5, 0, 2, 3, 1] have a sum divisible by 5?
- Finding the k-th smallest element with a median-of-medians pivot is used as a deterministic selection method. What is its worst-case time…
- Four sorted lists of sizes 7, 5, 3 and 2 are merged pairwise, always merging the two currently smallest lists first. What is the total…
- How many non-empty contiguous subarrays of [3, 5, 6, 1, 2, 3, 4] have bitwise XOR equal to 0?
- How many non-empty contiguous subarrays of [2, 2, 3, 3, 3, 2] consist of one repeated value only?
- How many non-empty contiguous subarrays of [1, 2, 4, 3, 6, 5, 7, 8] contain equally many even and odd numbers?