values. Call a set of its elements (not necessarily adjacent) a *ladder* if, once sorted in increasing order, every number except the first is the square of the one before it. For example, [2, 4, 16] is a ladder. A ladder must contain at least two numbers.
Return the length of the longest ladder you can build from the array's elements, or -1 if no ladder exists.values=[9, 5, 81, 3, 25, 7] → 3
Take 3, 9 and 81: 9 = 3² and 81 = 9². The chain 5 → 25 is shorter.
values=[10, 11, 12] → -1
No number is the square of another, so there is no ladder.
values=[4, 2, 4] → 2
The ladder is 2 → 4; the second 4 does not make it longer.
values) ≤ 10⁵values[i] ≤ 10⁹values=[9, 5, 81, 3, 25, 7]
3
Run checks the first 3 tests in your browser. Submit checks all 55 tests on the server, hidden ones included.