ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
def can_complete(gas, cost):
total = tank = start = 0
for i in range(len(gas)):
d = gas[i] - cost[i]
total += d
tank += d
if tank < 0:
start = i + 1
tank = 0
return start if total >= 0 else -1
print(can_complete([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]), can_complete([2, 3, 4], [3, 4, 3]))- A. 2 -1
- B. -1 3
- C. 3 0
- D. 3 -1
Correct answer: D. 3 -1
If total gas is below total cost, no full circuit exists and the answer is -1. Otherwise the start moves past each failed tank, and in the first case the start is station 3.
More Python Programming questions
- 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))
- What is printed? python import itertools print(sum(b - a for a, b in itertools.pairwise([1, 4, 2, 8])))
- What is printed? python g = [[0] 3] 2 g[0][1] = 5 h = [[0] 3 for in range(2)] h[0][1] = 5 print(g, h)
- What is printed? python import heapq h = [] for w, name in [(2, 'b'), (1, 'z'), (2, 'a'), (1, 'c')]: heapq.heappush(h, (w, name)) out = []…