Data Structures & AlgorithmsModerateTCS NQT · two-pointer warm-up
Problem: How many pairs (i < j) of [1, 3, 5, 7, 9] have a[i] + a[j] >= 10? Run the code.
Problem: How many pairs (i < j) of [1, 3, 5, 7, 9] have a[i] + a[j] >= 10? Run the code.
a = [1, 3, 5, 7, 9] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] + a[j] >= 10))What is printed?
- A. 6
- B. 4
- C. 12
- D. 9
Correct answer: A. 6
The pairs with sum ≥ 10 are (1,9), (3,7), (3,9), (5,7), (5,9) and (7,9): 6. Using > instead of ≥ drops the two sums of exactly 10 (4), counting ordered pairs gives 12, and starting j at i adds the self-pairs 5+5, 7+7, 9+9 (9).
More Coding Interview Problems questions
- Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code. python a = [1, -2, 3, 4, -1]…
- Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code. python sq = {x x for x in range(1, 11)} cu = {x…
- Problem: What is the sum of the digits of 2^10 = 1024? Run the code. python print(sum(int(c) for c in str(2 10))) What is printed?
- Problem: What is the product of the distinct elements of [2, 2, 3, 4, 4]? Run the code. python a = [2, 2, 3, 4, 4] p = 1 for x in set(a)…
- Problem: What is the minimum absolute difference between two values of the sorted list [1, 5, 6, 9]? Run the code. python a = sorted([1…
- Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code. python from itertools import combinations…