Predict the output
What does this print?
queens = [(0, 1), (1, 3), (2, 0), (3, 2)]
print(len({r - c for r, c in queens}), len({r + c for r, c in queens}))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4 4
Right: no two queens share either kind of diagonal, so this 4 × 4 board is a valid placement.
Two set sizes equal to n confirm a board in O(n), and the same keys let the search check each square in O(1).
Why not the others?
- B 3 4. The four values of row - col are -1, -2, 2 and 1, all different.
- C 4 3. The four values of row + col are 1, 4, 2 and 5, all different.