Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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,. i. e. if you select ai and aj, i * j must be a perfect square.
Return the sum of the complete subset with the maximum sum.
Example 1:
Input: nums = [8,7,3,5,7,2,4,9]
Output: 16
Explanation:
We select elements at indices 2 and 8 and 2 * 8 is a perfect square.
Example 2:
Input: nums = [8,10,3,8,1,13,7,9,4]
Output: 20
Explanation:
We select elements at indices 1, 4, and 9. 1 * 4, 1 * 9, 4 * 9 are perfect squares.
Constraints:
1 <= n == nums.length <= 1041 <= nums[i] <= 109Problem summary: 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,. i. e. if you select ai and aj, i * j must be a perfect square. Return the sum of the complete subset with the maximum sum.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[8,7,3,5,7,2,4,9]
[8,10,3,8,1,13,7,9,4]
constrained-subsequence-sum)maximum-alternating-subsequence-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
class Solution {
public long maximumSum(List<Integer> nums) {
long ans = 0;
int n = nums.size();
boolean[] used = new boolean[n + 1];
int bound = (int) Math.floor(Math.sqrt(n));
int[] squares = new int[bound + 1];
for (int i = 1; i <= bound + 1; i++) {
squares[i - 1] = i * i;
}
for (int i = 1; i <= n; i++) {
long res = 0;
int idx = 0;
int curr = i * squares[idx];
while (curr <= n) {
res += nums.get(curr - 1);
curr = i * squares[++idx];
}
ans = Math.max(ans, res);
}
return ans;
}
}
// Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
func maximumSum(nums []int) (ans int64) {
n := len(nums)
for k := 1; k <= n; k++ {
var t int64
for j := 1; k*j*j <= n; j++ {
t += int64(nums[k*j*j-1])
}
ans = max(ans, t)
}
return
}
# Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
class Solution:
def maximumSum(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
for k in range(1, n + 1):
t = 0
j = 1
while k * j * j <= n:
t += nums[k * j * j - 1]
j += 1
ans = max(ans, t)
return ans
// Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
// class Solution {
// public long maximumSum(List<Integer> nums) {
// long ans = 0;
// int n = nums.size();
// boolean[] used = new boolean[n + 1];
// int bound = (int) Math.floor(Math.sqrt(n));
// int[] squares = new int[bound + 1];
// for (int i = 1; i <= bound + 1; i++) {
// squares[i - 1] = i * i;
// }
// for (int i = 1; i <= n; i++) {
// long res = 0;
// int idx = 0;
// int curr = i * squares[idx];
// while (curr <= n) {
// res += nums.get(curr - 1);
// curr = i * squares[++idx];
// }
// ans = Math.max(ans, res);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2862: Maximum Element-Sum of a Complete Subset of Indices
function maximumSum(nums: number[]): number {
let ans = 0;
const n = nums.length;
for (let k = 1; k <= n; ++k) {
let t = 0;
for (let j = 1; k * j * j <= n; ++j) {
t += nums[k * j * j - 1];
}
ans = Math.max(ans, t);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.