Maximum Element-Sum of a Complete Subset of Indices - Problem

You are given a 1-indexed array nums. Your task is to find the complete subset with the maximum sum, where a complete subset is defined as a collection of indices where every pair of indices when multiplied results in a perfect square.

More formally, if you select indices i and j in your subset, then i * j must be a perfect square (like 1, 4, 9, 16, 25, etc.).

Goal: Return the maximum sum of elements from such a complete subset.

Example: For array [8, 7, 3, 5, 7, 2, 4, 9], indices 1 and 4 can be selected together because 1 * 4 = 4 (perfect square), giving us elements 8 + 5 = 13.

Input & Output

example_1.py โ€” Python
$ Input: [8, 7, 3, 5, 7, 2, 4, 9]
โ€บ Output: 35
๐Ÿ’ก Note: The indices are grouped by square-free signatures: {1,4} have signature 1, {2,8} have signature 2, {3} has signature 3, etc. The complete subsets are {1,4}, {2,8}, {3}, {5}, {6}, {7}. Maximum sum = 8+5 + 7+9 + 3 + 7 + 2 + 4 = 35.
example_2.py โ€” Python
$ Input: [5, 10, 3, 10, 1, 13, 7, 9, 4]
โ€บ Output: 50
๐Ÿ’ก Note: Grouping by square-free signatures: indices {1,4,9} all have signature 1 (since 1ร—1=1, 1ร—4=4, 1ร—9=9, 4ร—4=16, 4ร—9=36, 9ร—9=81 are all perfect squares). Sum = 5+10+4 = 19 for this group, plus individual elements from other groups.
example_3.py โ€” Python
$ Input: [1]
โ€บ Output: 1
๐Ÿ’ก Note: Edge case with single element. Index 1 forms a complete subset by itself (no pairs to check). Answer is simply nums[0] = 1.

Visualization

Tap to expand
๐ŸŽญ Theater Seating Harmony1$82$73$34$55$76$2Harmony Group 11ร—4=4=2ยฒ โœ“Harmony Groups by Square-Free Signatureโ— Group 1 (signature=1): Seats 1,4 โ†’ Revenue: $8+$5 = $13โ— Group 2 (signature=2): Seat 2 โ†’ Revenue: $7โ— Group 3 (signature=3): Seat 3 โ†’ Revenue: $3Total Maximum Revenue$13 + $7 + $3 + $7 + $2 + $4 = $36All harmony groups can coexist for maximum profit!
Understanding the Visualization
1
Identify Harmony Groups
Seats 1,4,9,16... have harmony signature 1 and can all sit together
2
Group by Signature
Seats 2,8,18,32... have harmony signature 2 and form another group
3
Calculate Group Values
Sum the ticket prices within each harmony group
4
Maximize Revenue
Add up all group totals for maximum theater revenue
Key Takeaway
๐ŸŽฏ Key Insight: Indices with the same square-free factorization signature can all be selected together, forming complete subsets where every pair multiplies to a perfect square. This mathematical property allows us to group indices efficiently and sum their corresponding array elements for the optimal solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nโˆšm)

We process each of n indices, and computing square-free part takes O(โˆšm) where m is the maximum index value

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for hash map to store groups of indices by their square-free signature

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • 1 โ‰ค nums[i] โ‰ค 109
  • Array is 1-indexed for the purpose of computing products
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
54.0K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen