ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
def inv(a):
s, c = [], 0
for x in a:
c += len(s) - bisect.bisect_right(s, x)
bisect.insort(s, x)
return c
print(inv([3, 1, 2, 3, 0]))- A. 6
- B. 7
- C. 10
- D. 2
Correct answer: A. 6
Each value adds the count of earlier values strictly greater than it (bisect_right): pairs (3,1), (3,2), (3,0), (1,0), (2,0) and the second (3,0) make 6. bisect_left would also count the equal pair (3,3) for 7, 10 is C(5,2) and 2 counts only adjacent inversions.
More Python Programming questions
- What is printed? python import bisect a = [10, 20, 20, 30] print([bisect.bisectleft(a, x) for x in (20, 25, 5, 40)])
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def grid(r, c): if r == 0 or c == 0: return 1 return grid(r…
- What is printed? python def cancomplete(gas, cost): total = tank = start = 0 for i in range(len(gas)): d = gas[i] - cost[i] total += d…
- What is printed? python x = 0b101100 print(x & (x - 1), x & -x, bin(x).count('1'))
- What is printed? python M = 1000000007 print(pow(2, -1, 7), pow(3, 4, 5), (pow(10, 9, M) 2) % M)
- What is printed? python import math print(math.comb(10, 3), math.gcd(12, 18, 24), math.lcm(4, 6, 10))