Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many inversions does the array [4, 3, 2, 1, 0] have? Run the code.
Problem: How many inversions does the array [4, 3, 2, 1, 0] have? Run the code.
a = [4, 3, 2, 1, 0] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] > a[j]))What is printed?
- A. 9
- B. 10
- C. 8
- D. 15
Correct answer: B. 10
A strictly decreasing array of length 5 has every pair inverted, giving C(5,2) = 10.
More Coding Interview Problems questions
- Problem: How many pairs (i < j) of [5, 6, 7, 9, 8] satisfy |ai - aj| = 1? Run the code. python a = [5, 6, 7, 9, 8] print(sum(1 for i in…
- Problem: How many integers in [1, 500] are multiples of 3 but not of 5? Run the code. python print(sum(1 for x in range(1, 501) if x % 3…
- Problem: How many non-negative integer pairs (x, y) satisfy 2x + 3y = 12? Run the code. python print(sum(1 for x in range(0, 7) for y in…
- Problem: Which algorithm solves the minimum-cost perfect assignment problem (bipartite, n x n cost matrix) in O(n^3) time?
- Problem: What is the complexity of Dinic's algorithm on a unit-capacity bipartite matching graph with V vertices and E edges?
- Problem: In Bellman-Ford, which statement about early termination is correct?