Problem Β· Combinatorics
The Three Knights π
Learn this problemProblem statement
Given a grid with n rows and m columns, count the possible placements of three knights such that no two knights attack each other. No two knights may occupy the same cell.
A knight at (a1, b1) attacks (a2, b2) when either:
|a1 - a2| = 1and|b1 - b2| = 2, or|a1 - a2| = 2and|b1 - b2| = 1.
Return the number of unordered three-cell placements in which no pair of cells is a knight's move apart.
Function
countPlacements(n: int, m: int) β longExamples
Example 1
n = 2m = 3return = 12There are 20 ways to choose three of the six cells. The board has two attacking cell pairs, and each pair appears with four possible third cells. Therefore 20 - 2 * 4 = 12 placements are valid.
Constraints
1 <= n1 <= mn * m <= 10^6
More Tiktok problems
- Count Access Code PairsOA Β· Seen Jul 2026
- Count Key ChangesOA Β· Seen Jul 2026
- Travel Distance on ScootersOA Β· Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA Β· Seen Jul 2026
- Obstacle Placement QueriesOA Β· Seen Jul 2026
- Repeated Grouped Digit SumOA Β· Seen Jul 2026
- Count Cyclic Digit PairsOA Β· Seen Jun 2026
- Event ID Check Completion TimesOA Β· Seen Jun 2026