ProgrammingHardGATE CS · Campus placements
For the array {4, 1, 3}, how many index pairs i < j have a[i] > a[j]? These pairs are called inversions.
- A. 1
- B. 2
- C. 3
- D. 0
Correct answer: B. 2
The inversions are (4,1) and (4,3), so there are 2. Merge sort counts inversions in O(n log n) by counting during each merge.
More C Programming questions
- How many binary strings of length 5 contain no two adjacent 1s?
- char s[] = "abc"; s[0] = 'x'; printf("%s", s); prints what?
- unsigned int n = 1u; unsigned int r = n << 31; What is r?
- int a = -7, b = 2; What is a / b b + a % b?
- int m[2][2] = {{1, 2}, {3, 4}}; int (r)[2] = m; What is r[1][0] + (r[0] + 1)?
- uint32t n = 100000u; uint32t t = n (n + 1) / 2; What is t?