All problems
0833MediumArrayHash TableEnumeration

Longest Mirrored Power Run

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3020Find the Maximum Number of Elements in Subset

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A test rig logs gain readings in readings. An engineer wants to pick a group of those readings, keeping no more copies of a value than the log holds, and lay the picked readings in a row.

The row has to be shaped like this. It opens on some reading. Each step towards the middle squares the reading before it. One reading sits alone in the middle, the largest in the row. After the middle the row descends through exactly the readings it climbed, in reverse, so it reads the same forwards and backwards. A row of a single reading is allowed, with that reading serving as its own middle.

Return the largest number of readings such a row can hold.

Examples

Example 1

Input
readings = [3, 9, 3, 81, 7]
Output
3

Laying 3, 9, 3 gives a row that climbs from 3 to its square 9 and comes back down, and the log holds two copies of 3 and one of 9. That row holds three readings.

Example 2

Input
readings = [5, 6]
Output
1

No value appears twice, so no row can climb. A single reading is a valid row, so the answer is 1.

Example 3

Input
readings = [2, 2, 4, 4, 16]
Output
5

Laying 2, 4, 16, 4, 2 uses both copies of 2, both copies of 4 and the single 16 as the middle reading, so the row holds five readings.

Constraints

  • 2 <= readings.length <= 10^5
  • 1 <= readings[i] <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_mirror_run(readings: list[int]) -> int:
Java
public int longestMirrorRun(int[] readings)
September 7
Apply