Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 9Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Backtracking · Greedy · Bit Manipulation
[3,-1,-5,2,5,-9]
[-4,-5,-4]
maximum-strength-of-k-disjoint-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2708: Maximum Strength of a Group
class Solution {
public long maxStrength(int[] nums) {
long ans = (long) -1e14;
int n = nums.length;
for (int i = 1; i < 1 << n; ++i) {
long t = 1;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
// Accepted solution for LeetCode #2708: Maximum Strength of a Group
func maxStrength(nums []int) int64 {
ans := int64(-1e14)
for i := 1; i < 1<<len(nums); i++ {
var t int64 = 1
for j, x := range nums {
if i>>j&1 == 1 {
t *= int64(x)
}
}
ans = max(ans, t)
}
return ans
}
# Accepted solution for LeetCode #2708: Maximum Strength of a Group
class Solution:
def maxStrength(self, nums: List[int]) -> int:
ans = -inf
for i in range(1, 1 << len(nums)):
t = 1
for j, x in enumerate(nums):
if i >> j & 1:
t *= x
ans = max(ans, t)
return ans
// Accepted solution for LeetCode #2708: Maximum Strength of a Group
/**
* [2708] Maximum Strength of a Group
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn max_strength(nums: Vec<i32>) -> i64 {
if nums.len() == 1 {
return nums[0] as i64;
}
let mut nums = nums;
nums.sort();
let mut now = 0;
let mut result = 1;
let mut found = false;
while now < nums.len() {
if nums[now] > 0 {
break;
}
if now + 1 < nums.len() && nums[now] * nums[now + 1] > 0 {
let r = nums[now] * nums[now + 1];
result *= r as i64;
found = true;
now += 2;
} else {
now += 1;
}
}
while now < nums.len() {
result *= nums[now] as i64;
found = true;
now += 1;
}
match found {
true => result,
false => 0,
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2708() {
assert_eq!(20, Solution::max_strength(vec![-4, -5, -4]));
assert_eq!(1350, Solution::max_strength(vec![3, -1, -5, 2, 5, -9]));
}
}
// Accepted solution for LeetCode #2708: Maximum Strength of a Group
function maxStrength(nums: number[]): number {
let ans = -Infinity;
const n = nums.length;
for (let i = 1; i < 1 << n; ++i) {
let t = 1;
for (let j = 0; j < n; ++j) {
if ((i >> j) & 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.