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
Visualization
Time & Space Complexity
We process each of n indices, and computing square-free part takes O(โm) where m is the maximum index value
Space for hash map to store groups of indices by their square-free signature
Constraints
- 1 โค nums.length โค 104
- 1 โค nums[i] โค 109
- Array is 1-indexed for the purpose of computing products