#2708
Medium Algorithms Maximum strength of a group
Array Dynamic Programming Backtracking Greedy Bit Manipulation Sorting Enumeration
25.6% acceptance
Feb 25, 2026
384
67
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.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn max_strength(nums: Vec<i32>) -> i64 {
let n = nums.len();
let mut best = i64::MIN;
for mask in 1u32..(1 << n) {
let mut prod: i64 = 1;
for i in 0..n {
if mask & (1 << i) != 0 {
prod *= nums[i] as i64;
}
}
best = best.max(prod);
}
best
}
}