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 0-indexed integer array nums representing the strength of some heroes. The power of a group of heroes is defined as follows:
i0, i1, ... ,ik be the indices of the heroes in a group. Then, the power of this group is max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik]).Return the sum of the power of all non-empty groups of heroes possible. Since the sum could be very large, return it modulo 109 + 7.
Example 1:
Input: nums = [2,1,4] Output: 141 Explanation: 1st group: [2] has power = 22 * 2 = 8. 2nd group: [1] has power = 12 * 1 = 1. 3rd group: [4] has power = 42 * 4 = 64. 4th group: [2,1] has power = 22 * 1 = 4. 5th group: [2,4] has power = 42 * 2 = 32. 6th group: [1,4] has power = 42 * 1 = 16. 7th group: [2,1,4] has power = 42 * 1 = 16. The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.
Example 2:
Input: nums = [1,1,1] Output: 7 Explanation: A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: You are given a 0-indexed integer array nums representing the strength of some heroes. The power of a group of heroes is defined as follows: Let i0, i1, ... ,ik be the indices of the heroes in a group. Then, the power of this group is max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik]). Return the sum of the power of all non-empty groups of heroes possible. Since the sum could be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[2,1,4]
[1,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2681: Power of Heroes
class Solution {
public int sumOfPower(int[] nums) {
final int mod = (int) 1e9 + 7;
Arrays.sort(nums);
long ans = 0, p = 0;
for (int i = nums.length - 1; i >= 0; --i) {
long x = nums[i];
ans = (ans + (x * x % mod) * x) % mod;
ans = (ans + x * p % mod) % mod;
p = (p * 2 + x * x % mod) % mod;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #2681: Power of Heroes
func sumOfPower(nums []int) (ans int) {
const mod = 1e9 + 7
sort.Ints(nums)
p := 0
for i := len(nums) - 1; i >= 0; i-- {
x := nums[i]
ans = (ans + (x*x%mod)*x) % mod
ans = (ans + x*p%mod) % mod
p = (p*2 + x*x%mod) % mod
}
return
}
# Accepted solution for LeetCode #2681: Power of Heroes
class Solution:
def sumOfPower(self, nums: List[int]) -> int:
mod = 10**9 + 7
nums.sort()
ans = 0
p = 0
for x in nums[::-1]:
ans = (ans + (x * x % mod) * x) % mod
ans = (ans + x * p) % mod
p = (p * 2 + x * x) % mod
return ans
// Accepted solution for LeetCode #2681: Power of Heroes
// 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 #2681: Power of Heroes
// class Solution {
// public int sumOfPower(int[] nums) {
// final int mod = (int) 1e9 + 7;
// Arrays.sort(nums);
// long ans = 0, p = 0;
// for (int i = nums.length - 1; i >= 0; --i) {
// long x = nums[i];
// ans = (ans + (x * x % mod) * x) % mod;
// ans = (ans + x * p % mod) % mod;
// p = (p * 2 + x * x % mod) % mod;
// }
// return (int) ans;
// }
// }
// Accepted solution for LeetCode #2681: Power of Heroes
function sumOfPower(nums: number[]): number {
const mod = 10 ** 9 + 7;
nums.sort((a, b) => a - b);
let ans = 0;
let p = 0;
for (let i = nums.length - 1; i >= 0; --i) {
const x = BigInt(nums[i]);
ans = (ans + Number((x * x * x) % BigInt(mod))) % mod;
ans = (ans + Number((x * BigInt(p)) % BigInt(mod))) % mod;
p = Number((BigInt(p) * 2n + x * x) % BigInt(mod));
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.