Data Structures & AlgorithmsModerateTCS NQT · number theory
Problem: How many pairs (i < j) of [2, 4, 6, 8] have gcd exactly 2? Run the code.
Problem: How many pairs (i < j) of [2, 4, 6, 8] have gcd exactly 2? Run the code.
from itertools import combinations from math import gcd print(sum(1 for i, j in combinations([2, 4, 6, 8], 2) if gcd(i, j) == 2))What is printed?
- A. 4
- B. 5
- C. 6
- D. 3
Correct answer: B. 5
The qualifying pairs are (2,4), (2,6), (2,8), (4,6) and (6,8). Only (4,8) has gcd 4.
More Coding Interview Problems questions
- Problem: Starting at (0,0) in the grid below, how many cells are reachable by 4-directional moves through '.' cells? Run the BFS. python…
- Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP. python INF = 10 9 d = [0] + [INF] 63 for v in…
- Problem: Climbing stairs 0 to 5 with steps of 1 or 2, but stair 3 is broken and cannot be stood on. How many ways are there to reach stair…
- Problem: Is 'cdeab' a rotation of 'abcde'? Check with the substring test below. What is printed? python s, t = 'abcde', 'cdeab'…
- Problem: Find the minimum cost of a Hamiltonian tour starting and ending at city 0, with symmetric distances D =…
- Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code. python print(sum(1 for a in range(1, 7) for b in…