Data Structures & AlgorithmsHard
How many contiguous subarrays of length at least 2 of 3 1 4 2 5 contain an EVEN number of inversions (pairs i < j inside the subarray with a[i] > a[j])? Zero counts as even.
- A. 8
- B. 7
- C. 3
- D. 2
Correct answer: D. 2
Each block's inversions are counted inside the block only, and zero is even so ascending blocks qualify. Flipping the parity, including singletons, or counting only the sorted blocks each change the answer.
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
- For 2 3 5 6 4 1 3, let Pi be the product of A[0..i] modulo 7 and Si the product of A[i+1..n−1] modulo 7. How many indices i with 0 ≤ i ≤…
- How many subsets of exactly three elements of 1 2 3 4 5 6 (chosen by position) have XOR equal to 0?
- How many index pairs i < j in 1 2 3 4 5 6 7 8 have A[i] XOR A[j] equal to a POWER OF TWO (1, 2, 4, 8, …)?
- How many NON-EMPTY subsets of {1, 2, ..., 10} have a sum divisible by 10?
- How many NON-EMPTY subsets of 2 3 5 7 11 13 have a product that is at most 60?
- Among all arrays of length 4 with values in {1, 2, 3, 4}, the absolute differences between ADJACENT elements (three of them) must all be…