2708. Maximum Strength of a Group LeetCode Solution
In this guide, you will get 2708. Maximum Strength of a Group LeetCode Solution with the best time and space complexity. The solution to Maximum Strength of a Group 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
- Maximum Strength of a Group solution in C++
- Maximum Strength of a Group solution in Java
- Maximum Strength of a Group solution in Python
- Additional Resources
Problem Statement of Maximum Strength of a Group
You are given a 0-indexed integer array nums representing the score of students in an exam. The teacher would like to form one non-empty group of students with maximal strength, where the strength of a group of students of indices i0, i1, i2, … , ik is defined as nums[i0] * nums[i1] * nums[i2] * … * nums[ik].
Return the maximum strength of a group the teacher can create.
Example 1:
Input: nums = [3,-1,-5,2,5,-9]
Output: 1350
Explanation: One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.
Example 2:
Input: nums = [-4,-5,-4]
Output: 20
Explanation: Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
Constraints:
1 <= nums.length <= 13
-9 <= nums[i] <= 9
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2708. Maximum Strength of a Group LeetCode Solution in C++
class Solution {
public:
long long maxStrength(vector<int>& nums) {
long posProd = 1;
long negProd = 1;
int maxNeg = INT_MIN;
int negCount = 0;
bool hasPos = false;
bool hasZero = false;
for (const int num : nums)
if (num > 0) {
posProd *= num;
hasPos = true;
} else if (num < 0) {
negProd *= num;
maxNeg = max(maxNeg, num);
++negCount;
} else { // num == 0
hasZero = true;
}
if (negCount == 0 && !hasPos)
return 0;
if (negCount % 2 == 0)
return negProd * posProd;
if (negCount >= 3)
return negProd / maxNeg * posProd;
if (hasPos)
return posProd;
if (hasZero)
return 0;
return maxNeg;
}
};
/* code provided by PROGIEZ */
2708. Maximum Strength of a Group LeetCode Solution in Java
class Solution {
public long maxStrength(int[] nums) {
long posProd = 1;
long negProd = 1;
int maxNeg = Integer.MIN_VALUE;
int negCount = 0;
boolean hasPos = false;
boolean hasZero = false;
for (final int num : nums)
if (num > 0) {
posProd *= num;
hasPos = true;
} else if (num < 0) {
negProd *= num;
maxNeg = Math.max(maxNeg, num);
++negCount;
} else { // num == 0
hasZero = true;
}
if (negCount == 0 && !hasPos)
return 0;
if (negCount % 2 == 0)
return negProd * posProd;
if (negCount >= 3)
return negProd / maxNeg * posProd;
if (hasPos)
return posProd;
if (hasZero)
return 0;
return maxNeg;
}
}
// code provided by PROGIEZ
2708. Maximum Strength of a Group LeetCode Solution in Python
class Solution:
def maxStrength(self, nums: list[int]) -> int:
posProd = 1
negProd = 1
maxNeg = -math.inf
negCount = 0
hasPos = False
hasZero = False
for num in nums:
if num > 0:
posProd *= num
hasPos = True
elif num < 0:
negProd *= num
maxNeg = max(maxNeg, num)
negCount += 1
else: # num == 0
hasZero = True
if negCount == 0 and not hasPos:
return 0
if negCount % 2 == 0:
return negProd * posProd
if negCount >= 3:
return negProd // maxNeg * posProd
if hasPos:
return posProd
if hasZero:
return 0
return maxNeg
# 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.