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.
In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you.
You have been cursed in such a way that after absorbing energy from magician i, you will be instantly transported to magician (i + k). This process will be repeated until you reach the magician where (i + k) does not exist.
In other words, you will choose a starting point and then teleport with k jumps until you reach the end of the magicians' sequence, absorbing all the energy during the journey.
You are given an array energy and an integer k. Return the maximum possible energy you can gain.
Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
Example 1:
Input: energy = [5,2,-10,-5,1], k = 3
Output: 3
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
Example 2:
Input: energy = [-2,-3,-1], k = 2
Output: -1
Explanation: We can gain a total energy of -1 by starting from magician 2.
Constraints:
1 <= energy.length <= 105-1000 <= energy[i] <= 10001 <= k <= energy.length - 1Problem summary: In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you. You have been cursed in such a way that after absorbing energy from magician i, you will be instantly transported to magician (i + k). This process will be repeated until you reach the magician where (i + k) does not exist. In other words, you will choose a starting point and then teleport with k jumps until you reach the end of the magicians' sequence, absorbing all the energy during the journey. You are given an array energy and an integer k. Return the maximum possible energy you can gain. Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[5,2,-10,-5,1] 3
[-2,-3,-1] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3147: Taking Maximum Energy From the Mystic Dungeon
class Solution {
public int maximumEnergy(int[] energy, int k) {
int ans = -(1 << 30);
int n = energy.length;
for (int i = n - k; i < n; ++i) {
for (int j = i, s = 0; j >= 0; j -= k) {
s += energy[j];
ans = Math.max(ans, s);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3147: Taking Maximum Energy From the Mystic Dungeon
func maximumEnergy(energy []int, k int) int {
ans := -(1 << 30)
n := len(energy)
for i := n - k; i < n; i++ {
for j, s := i, 0; j >= 0; j -= k {
s += energy[j]
ans = max(ans, s)
}
}
return ans
}
# Accepted solution for LeetCode #3147: Taking Maximum Energy From the Mystic Dungeon
class Solution:
def maximumEnergy(self, energy: List[int], k: int) -> int:
ans = -inf
n = len(energy)
for i in range(n - k, n):
j, s = i, 0
while j >= 0:
s += energy[j]
ans = max(ans, s)
j -= k
return ans
// Accepted solution for LeetCode #3147: Taking Maximum Energy From the Mystic Dungeon
impl Solution {
pub fn maximum_energy(energy: Vec<i32>, k: i32) -> i32 {
let n = energy.len();
let mut ans = i32::MIN;
for i in n - k as usize..n {
let mut s = 0;
let mut j = i as i32;
while j >= 0 {
s += energy[j as usize];
ans = ans.max(s);
j -= k;
}
}
ans
}
}
// Accepted solution for LeetCode #3147: Taking Maximum Energy From the Mystic Dungeon
function maximumEnergy(energy: number[], k: number): number {
const n = energy.length;
let ans = -Infinity;
for (let i = n - k; i < n; ++i) {
for (let j = i, s = 0; j >= 0; j -= k) {
s += energy[j];
ans = Math.max(ans, s);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.