Data Structures & AlgorithmsVery hard
For pairs i < j with (a_i + a_j) divisible by an EVEN number m, with remainder counts c_r, which formula is correct?
- A. Σ over all r of c_r · c_(m−r) (summing over every residue)
- B. c_0·(c_0−1)/2 + c_(m/2)² + Σ over r from 1 to m/2−1 of c_r·c_(m−r)
- C. c_0·(c_0−1)/2 + Σ over r from 1 to m/2−1 of c_r·c_(m−r) + c_(m/2)·(c_(m/2)−1)/2
- D. Σ over r of c_r·(c_r − 1)
Correct answer: C. c_0·(c_0−1)/2 + Σ over r from 1 to m/2−1 of c_r·c_(m−r) + c_(m/2)·(c_(m/2)−1)/2
Residues pair up as r with m−r; residue 0 and residue m/2 pair within their own class, giving C(c,2). Summing over all r counts each unordered pair twice, and squaring c_(m/2) counts 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
- Which statement about counting subarrays with EXACTLY k distinct values is correct?
- Positive integers, two pointers count subarrays with product less than K. Why is right − left + 1 added for each right end?
- Which statement about inversion counting with repeated values is correct?
- Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are…
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…