Data Structures & AlgorithmsVery hardICPC regional · GATE CS
How many permutations of 1 to 6 have NO fixed point (that is, p[i] ≠ i for every position i)?
- A. 265
- B. 120
- C. 266
- D. 714
Correct answer: A. 265
Inclusion–exclusion over the set of fixed points gives D(n) = Σ (−1)^k C(n, k) (n−k)!, which equals 265 for n = 6. Counting permutations that avoid just one position gives a much larger wrong number.
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 strings of length 6 over {a, b} have NO position i with s[i] = s[i+2] (for every valid i)?
- How many arrays of length 4 with entries from {1, 2, ..., 3} are NON-INCREASING (each entry is at least the next)?
- Daily prices are [3, 2, 6, 5, 0, 3, 8, 1, 9]. You may complete at most 3 buy-then-sell transactions (one share at a time, sell before the…
- What is the maximum sum of a non-empty contiguous subarray of [4, -6, 3, -1, 7, -2, 5] that contains AT MOST one negative element?
- What is the maximum product of exactly 3 consecutive elements of [2, -3, 4, -1, 5, -6, 2]?
- How many contiguous subarrays (of length at least 2) of [1, 3, 4, 2, 5, 6, 8, 1] are strictly increasing?