Power of Heroes - Problem
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 i₀, i₁, ... ,iₖ be the indices of the heroes in a group. Then, the power of this group is max(nums[i₀], nums[i₁], ... ,nums[iₖ])² × min(nums[i₀], nums[i₁], ... ,nums[iₖ]).
Return the sum of the power of all non-empty groups of heroes possible. Since the sum could be very large, return it modulo 10⁹ + 7.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,1,4]
›
Output:
141
💡 Note:
All possible groups: [2]→8, [1]→1, [4]→64, [2,1]→4, [2,4]→32, [1,4]→16, [2,1,4]→16. Sum = 8+1+64+4+32+16+16 = 141
Example 2 — Minimum Size
$
Input:
nums = [1]
›
Output:
1
💡 Note:
Only one group [1] with power = 1² × 1 = 1
Example 3 — Equal Elements
$
Input:
nums = [3,3]
›
Output:
81
💡 Note:
Groups: [3] (first) → 27, [3] (second) → 27, [3,3] → 27. Sum = 27+27+27 = 81
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code