Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many ordered pairs (i < j) of [1, 2, 3, 4, 6] have a[j] divisible by a[i]? Run the code.
Problem: How many ordered pairs (i < j) of [1, 2, 3, 4, 6] have a[j] divisible by a[i]? Run the code.
a = [1, 2, 3, 4, 6] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[j] % a[i] == 0))What is printed?
- A. 12
- B. 7
- C. 3
- D. 10
Correct answer: B. 7
The qualifying pairs are (1,2), (1,3), (1,4), (1,6), (2,4), (2,6) and (3,6), giving 7; (2,3) and (3,4) fail. Starting j at i adds the 5 self-pairs (12), forgetting that 1 divides everything leaves only 3, and 10 counts all C(5,2) pairs as if divisibility always held.
More Coding Interview Problems questions
- Problem: What is the minimum number of operations to go from 1 to 10 when each operation is +1 or 2? Run the BFS. python from collections…
- Problem: How many binary strings of length 4 avoid the substring '11'? Run the code. python print(sum(1 for t in range(16) if '11' not in…
- Problem: What is the minimum number of insertions and deletions needed to turn 'abc' into 'adc'? (Only insertions and deletions are…
- Problem: What is the longest common prefix of the list ['flower', 'flow', 'flight']? Run the code. python import os…
- Problem: What does the following print? python print(' '.join('the sky is blue'.split()[::-1]))
- Problem: Which statement about Hall's theorem for a bipartite graph with sides L and R is correct?