Ctrl K

Positions With Square Products

Hard
You are given an array values whose elements are numbered **starting from 1**. Choose a non-empty group of positions such that the product of **any two** chosen positions is a perfect square (a group of one position always qualifies). Return the largest possible sum of the elements in the chosen group.
Example 1
values=[3, 1, 4, 6, 2, 5, 2, 8] → 9

Positions 1 and 4: 1·4 = 4 is a square, sum 3 + 6 = 9. Positions 2 and 8: 2·8 = 16, sum 1 + 8 = 9. The two groups cannot be merged (1·2 = 2 is not a square). The answer is 9.

Example 2
values=[2, 5, 7] → 7

No two positions multiply to a square, so a single element is taken: the largest one, 7.

Example 3
values=[1, 1, 1, 1, 1, 1, 1, 1, 1] → 3
Constraints
  • 1 ≤ len(values) ≤ 10⁴
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Shared-Factor Group HardMake One From Multiples HardTowns Joined by Big Divisors HardNearest Coprime Ancestor HardDistinct GCDs of Selections Hard
loading Python…
Input
values=[3, 1, 4, 6, 2, 5, 2, 8]
Expected
9

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