Data Structures & AlgorithmsVery hard
When an array's values are all distinct, what is the number of ordered pairs (i, j), i ≠ j, with A[i] > A[j]?
- A. n²
- B. n(n+2)
- C. n(n−1)/2
- D. n(n−1)
Correct answer: C. n(n−1)/2
Each unordered pair of distinct values contributes exactly one ordered pair with the larger value first, giving C(n,2) = n(n−1)/2. The other formulas count both orders or include the diagonal.
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
- Push and pop operations on an initially empty stack are: push, push, pop, push, push, push, pop, pop, push. What is the maximum height the…
- Customers arrive at times 0, 1, 2 and 5 and need service times 3, 2, 1 and 2 on a single server (first come, first served; a customer…
- For the same four customers (arrivals 0, 1, 2, 5; service times 3, 2, 1, 2) on a single first-come-first-served server, what is the…
- A list holds 1 to 9 in order. The first 4 nodes are reversed, then the node with value 6 is deleted. What is the value at position 5 of…
- How many contiguous subarrays of [1, 2, -3, 3, -3, 2, 1] have sum exactly zero?
- How many index pairs i < j in [1, 3, 5, 7, 9, 4] have |a[i] - a[j]| = 2?