Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(ai, aj) = 1? Run the code.
Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(a_i, a_j) = 1? Run the code.
from math import gcd a = [6, 10, 15, 21] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if gcd(a[i], a[j]) == 1))What is printed?
- A. 2
- B. 1
- C. 0
- D. 3
Correct answer: B. 1
gcd is 1 exactly when the two numbers share no prime factor, which the pairwise check verifies.
More Coding Interview Problems questions
- Problem: What is the smallest positive integer that is divisible by every integer from 1 to 12? Run the code. python from math import lcm…
- Problem: Which technique gives the number of distinct subarrays of an array of length n in O(n log n) time using a suffix structure?
- Problem: Which statement about the Knuth optimization for interval DP is correct when its monotonicity conditions hold?
- Problem: What is the time complexity of building a suffix array with prefix doubling and radix sort?
- Problem: Which problem is solved by the Aho-Corasick automaton?
- Problem: Which claim about the centroid decomposition of a tree is correct?