1. Base case
A one-element range is its own maximum.
if (lo === hi)Typorithm · medium
Step 1 of 8 · intro card
Basic Patterns
Split the problem in half, solve each half, then combine the two answers.
Divide: cut the range at the midpoint into two independent halves.
Conquer: solve each half recursively until ranges shrink to one element.
Combine: merge sub-answers — here a single Math.max of two values.
Three phases: divide, conquer, combine.
Halves are independent subproblems.
The combine step defines the algorithm.
function maxValue(arr, lo, hi) {
if (lo === hi) {
return arr[lo];
}
const mid = Math.floor((lo + hi) / 2);
const left = maxValue(arr, lo, mid);
const right = maxValue(arr, mid + 1, hi);
return Math.max(left, right);
}Connect each part of the code to the concept before you type.
A one-element range is its own maximum.
if (lo === hi)Split the range into two halves at the middle.
const mid = Math.floor((lo + hi) / 2)Solve the left half as its own smaller problem.
const left = maxValue(arr, lo, mid)Solve the right half independently of the left.
const right = maxValue(arr, mid + 1, hi)Merge two sub-answers into the answer for the whole range.
return Math.max(left, right)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.