2681. Power of Heroes LeetCode Solution
In this guide, you will get 2681. Power of Heroes LeetCode Solution with the best time and space complexity. The solution to Power of Heroes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Power of Heroes solution in C++
- Power of Heroes solution in Java
- Power of Heroes solution in Python
- Additional Resources
Problem Statement of Power of Heroes
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.
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 <= 105
1 <= nums[i] <= 109
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
2681. Power of Heroes LeetCode Solution in C++
class Solution {
public:
int sumOfPower(vector<int>& nums) {
constexpr int kMod = 1'000'000'007;
long ans = 0;
long sum = 0;
ranges::sort(nums);
for (const int num : nums) {
ans = (ans + (num + sum) * num % kMod * num % kMod) % kMod;
sum = (sum * 2 + num) % kMod;
}
return ans;
}
};
/* code provided by PROGIEZ */
2681. Power of Heroes LeetCode Solution in Java
class Solution {
public int sumOfPower(int[] nums) {
final int kMod = 1_000_000_007;
long ans = 0;
long sum = 0;
Arrays.sort(nums);
for (final int num : nums) {
ans = (ans + (num + sum) * num % kMod * num % kMod) % kMod;
sum = (sum * 2 + num) % kMod;
}
return (int) ans;
}
}
// code provided by PROGIEZ
2681. Power of Heroes LeetCode Solution in Python
class Solution:
def sumOfPower(self, nums: list[int]) -> int:
kMod = 1_000_000_007
ans = 0
summ = 0
for num in sorted(nums):
ans += (num + summ) * num**2
ans %= kMod
summ = (summ * 2 + num) % kMod
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.