Data Structures & AlgorithmsHardTCS NQT · number theory
Problem: How many unordered pairs (i < j) of 1..10 have a sum divisible by 5? Run the code.
Problem: How many unordered pairs (i < j) of 1..10 have a sum divisible by 5? Run the code.
a = list(range(1, 11)) print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if (a[i] + a[j]) % 5 == 0))What is printed?
- A. 10
- B. 11
- C. 9
- D. 12
Correct answer: C. 9
Residues mod 5 pair up as (0,0), (1,4) and (2,3). They give 1 + 4 + 4 = 9 pairs, for example (5,10) and (1,9).
More Coding Interview Problems questions
- Problem: Minimum number of operations to reduce x = 5 to 0 by removing a number from either end of [1, 1, 4, 2, 3] (each removal subtracts…
- Problem: How many ordered sequences of 1s and 3s sum to 6? Run the code. python ways = [1] + [0] 6 for v in range(1, 7): for s in (1, 3)…
- Problem: What is the maximum XOR of any contiguous subarray of [8, 1, 2, 12]? Run the brute force. python a = [8, 1, 2, 12] best = 0 for i…
- Problem: Sort [1, 1, 2, 2, 2, 3] by frequency ascending, breaking ties by value ascending. What is printed? python from collections import…
- Problem: How many set bits are there in total across the numbers 0 to 7? Run the code. python print(sum(bin(i).count('1') for i in…
- Problem: How many integers in [1, 20] are powers of two? Run the code. python print(sum(1 for x in range(1, 21) if x & (x - 1) == 0)) What…