Ctrl K

Square-Sum Chains

Hard
Each bead carries a number; the numbers are given in the array beads. All the beads are strung into a chain in some order. A chain is pretty if the numbers on every two neighbouring beads add up to a perfect square. Return the number of different pretty chains. Two chains differ if some position holds different numbers in them; beads with the same number are indistinguishable. A chain of a single bead is always pretty.
Example 1
beads=[3, 6, 13] → 2

The chains [6, 3, 13] and [13, 3, 6] work: 6 + 3 = 9 and 3 + 13 = 16.

Example 2
beads=[8, 8, 1] → 3

8 + 8 = 16 and 8 + 1 = 9, so [1, 8, 8], [8, 1, 8] and [8, 8, 1] all work. Swapping the two equal 8s does not make a new chain.

Example 3
beads=[2, 5] → 0
Constraints
  • 1 ≤ len(beads) ≤ 12
  • 0 ≤ beads[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Lines Through the Dots MediumSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardBest Word Set From Tiles HardOrders of Identical Parts Hard
loading Python…
Input
beads=[3, 6, 13]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.