Data Structures & AlgorithmsVery hard
Why is k ≥ n a necessary condition for the residues of n values modulo k to be distinct?
- A. Residues are always larger than the number of elements.
- B. Modular arithmetic only works for prime k.
- C. There are only k possible residues, so n distinct residues need k ≥ n.
- D. k must divide the sum of the values.
Correct answer: C. There are only k possible residues, so n distinct residues need k ≥ n.
Each residue lies in {0, …, k−1}, a set of exactly k values, so n pairwise distinct residues require at least n of them. The question then asks for the first such k.
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
- When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?
- To count subarrays with equal numbers of even and odd values, which transformation makes the problem a zero-sum subarray problem?
- The function below computes the length of a longest increasing subsequence. python import bisect a = [4, 4, 1, 5, 5, 6, 2, 7] t = [] for x…
- A maximum subarray sum routine is written as follows. python def bestsub(a): cur = best = a[0] for x in a[1:]: cur = max(x, cur + x) best…
- A knapsack routine is written as follows (items are (weight, value)). python items = [(2, 3), (3, 4), (4, 5)] W = 6 dp = [0] (W + 1) for…
- Two DP loops count ways to pay 5 using coins 1 and 2 (the order of coins in the sum is ignored in the first version). python coins = [1…