Data Structures & AlgorithmsHardCodeforces Div2 C/D · Amazon SDE OA
Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50.
Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50.
from math import gcd print(sum(gcd(i,j) for i in range(1,51) for j in range(i+1,51)))What is printed?
- A. 2725
- B. 4000
- C. 592654
- D. 2676
Correct answer: A. 2725
Iterate over the unordered pairs and add their gcd; including i = j adds the diagonal, using lcm sums a different function, and skipping j = i+1 drops whole pairs.
More Coding Interview Problems questions
- How many integers n with 1 ≤ n ≤ 10000 have exactly four positive divisors? Count them with a divisor-count sieve. python N=10000…
- Array a = [3, 8, 2, 9, 5, 7, 6]. Count the index triples i < j < k such that the three values, sorted, are equally spaced (an arithmetic…
- Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force…
- Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code. python print(sum(1 for a in range(1,21) for…
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…
- A rod of length 8 can be cut into integer pieces, and a piece of length L sells for p[L] with p = [1, 5, 8, 9, 10, 17, 17, 20] for L =…