Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many pairs (i < j) of [2, 4, 6, 9, 12, 15] have gcd(ai, aj) = 3? Run the code.
Problem: How many pairs (i < j) of [2, 4, 6, 9, 12, 15] have gcd(a_i, a_j) = 3? Run the code.
from math import gcd a = [2, 4, 6, 9, 12, 15] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if gcd(a[i], a[j]) == 3))What is printed?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Only pairs whose elements are both multiples of 3 with no common factor of 2 have gcd exactly 3.
More Coding Interview Problems questions
- Problem: How many integers in [1, 300] are divisible by exactly two of the numbers 2, 3 and 5? Run the code. python print(sum(1 for x in…
- Problem: What is the minimum number of perfect cubes (1, 8, 27, ...) that sum to 100? Run the DP. python INF = 10 9 dp = [0] + [INF] 100…
- Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code. python print(sum(1 for x in…
- Problem: What is the length of the longest subarray of [1, 1, 0, 1, 1, 1, 0, 1] containing at most one zero? Run the sliding-window code…
- Problem: How many distinct products a b arise when a and b are each a face value from 1 to 6? Run the code. python print(len({a b for a in…
- Problem: How many subsets of {1, 2, ..., 6} have a product that is a perfect square? Run the code (the empty set has product 1). python…