[LeetCode] Maximum Strength of a Group

2708. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
long long maxStrength(vector<int>& nums) {
long long res = 1;
for(auto n : nums) res *= n;
for(int i = 1; i < 1<<(nums.size()); i++) {
long long now = 1;
for(int j = 0; j < nums.size(); j++) {
if(i & (1ll<<j)) now *= nums[j];
}
res = max(res, now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/28/PS/LeetCode/maximum-strength-of-a-group/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.