Ctrl K

Mirror of Squares

Medium
You get an array of positive integers values. Pick some of its elements (their positions in the array do not matter) and arrange them into a "mirror" row: x, x², x⁴, …, x^m, …, x⁴, x², x where m is a power of two (including m = 1, in which case the row is just x). Each number toward the middle is the square of the one before it; the middle appears once and every other value appears twice, symmetrically. For example, [2, 4, 16, 4, 2] and [5, 25, 5] qualify, while [2, 4, 8, 4, 2] does not. Return the largest number of elements such a row can have.
Example 1
values=[3, 9, 81, 9, 3, 2] → 5

Lay out 3, 9, 81, 9, 3: each number toward the middle is the square of the previous one. The 2 does not fit.

Example 2
values=[1, 1, 1, 1, 5] → 3

The ones form the mirror 1, 1, 1 (one squared is still one), but the length must be odd, so the fourth 1 is left out.

Example 3
values=[6, 7, 49, 10] → 1

7, 49, 7 would need a second 7, which is missing. Only a one-number mirror is left.

Constraints
  • 2 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGrid Points Under Circles MediumDistinct Segments With Few Multiples MediumDark Cells in 2×2 Windows MediumSquare Plot by Pulling Fences MediumPrimes Along Grid Rays Medium
loading Python…
Input
values=[3, 9, 81, 9, 3, 2]
Expected
5

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