---
# System prepended metadata

title: LC 2708. Maximum Strength of a Group
tags: [medium, leedcode, python]

---

# LC 2708. Maximum Strength of a Group

### [Problem link](https://leetcode.com/problems/maximum-strength-of-a-group/)

###### tags: `leedcode` `python` `medium`

You are given a  **0-indexed**  integer array <code>nums</code> 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 <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> is defined as <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code>.

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:** 

- <code>1 <= nums.length <= 13</code>
- <code>-9 <= nums[i] <= 9</code>


## Solution 1
```python=
class Solution:
    def maxStrength(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        
        neg = []
        pos = []
        for num in nums:
            if num > 0:
                pos.append(num)
            elif num < 0:
                neg.append(num)
        
        if len(neg) % 2:
            neg.remove(max(neg))
            
        if not neg and not pos:
            return 0
        
        res = 1
        for p in pos:
            res *= p
        for n in neg:
            res *= n
            
        return res
        
```

>### Complexity
>n = nums.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |


## Note
分成positive, negative處理.
