Maximum Element-Sum of a Complete Subset of Indices

You are given a 1-indexed array nums. Your task is to select a complete subset from nums where every pair of selected indices multiplied is a perfect square; that is, if you select nums[i] and nums[j], then i * j must be a perfect square.

Return the sum of the complete subset with the maximum sum.

Example 1
Inputnums = [8,7,3,5,7,2,4,9]
Output16
We select elements at indices 2 and 8, and 2 * 8 is a perfect square.
Example 2
Inputnums = [8,10,3,8,1,13,7,9,4]
Output20
We select elements at indices 1, 4, and 9; 1 * 4, 1 * 9, and 4 * 9 are perfect squares.

Constraints

  • 1 <= n == nums.length <= 10^4
  • 1 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate