ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from collections import Counter
def div_pairs(a, k):
c = Counter(x % k for x in a)
total = 0
for r in c:
s = (k - r) % k
if r < s:
total += c[r] * c[s]
elif r == s:
total += c[r] * (c[r] - 1) // 2
return total
print(div_pairs([1, 2, 3, 4, 5, 6], 3), div_pairs([3, 3, 3, 3], 3))- A. 4 6
- B. 5 4
- C. 6 6
- D. 5 6
Correct answer: D. 5 6
Group values by remainder mod k. Remainder 0 gives one pair from 3 and 6, and remainders 1 and 2 give 2 x 2 = 4 pairs, so 5 in total. Four values with remainder 0 give C(4,2) = 6 pairs.
More Python Programming questions
- What is printed? python def isrot(a, b): return len(a) == len(b) and b in a + a print(isrot('abcde', 'cdeab'), isrot('abc', 'acb')…
- What is printed? python def egcd(a, b): if b == 0: return a, 1, 0 g, x, y = egcd(b, a % b) return g, y, x - (a // b) y g, x, y = egcd(240…
- What is printed? python def canjump(a): far = 0 for i, x in enumerate(a): if i > far: return False far = max(far, i + x) return True…
- What is printed? python import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)
- What is printed? python def f(x, acc=[]): acc.append(x) return acc f(1) f(2) print(f(3, []), f(4))
- What is printed? python def gridpaths(r, c, memo={}): if r == 0 or c == 0: return 1 if (r, c) not in memo: memo[(r, c)] = gridpaths(r - 1…