Data Structures & AlgorithmsHard
To count ordered pairs (i, j), i ≠ j, with A[i] + A[j] ≡ 0 (mod 5), which correction turns an unordered-pair count into the ordered one?
- A. Add the number of elements, since each element pairs with itself.
- B. Square the unordered count.
- C. Halve the unordered count.
- D. Double the unordered count, since each unordered pair gives two ordered pairs when i ≠ j.
Correct answer: D. Double the unordered count, since each unordered pair gives two ordered pairs when i ≠ j.
For i ≠ j, each unordered pair {i, j} yields exactly two ordered pairs, so the ordered count is twice the unordered count. Self-pairs are excluded by the statement.
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 subarrays, why do two blocks with identical values but different positions count as two different subarrays?
- In the 'shortest window with a then b in order' problem, why does taking the nearest later 'b' for each 'a' suffice?
- Why is the maximum product of three values sometimes achieved by two large negatives and one large positive?
- For a binary string with no three equal consecutive bits, why is a run-length description convenient for counting?
- Why does the sliding-window approach fail to find the shortest subarray with sum divisible by m when values are NEGATIVE?
- Which property makes the ordered-pair count of A[i] > A[j] equal to the number of unequal unordered pairs?