Typorithm · medium

Divide & Conquer

divide-and-conquer-intropatternsdivide-and-conquerrecursioncombine
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Divide & Conquer

0%

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.

01

Three phases: divide, conquer, combine.

02

Halves are independent subproblems.

03

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);
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Base case

A one-element range is its own maximum.

if (lo === hi)

2. Divide point

Split the range into two halves at the middle.

const mid = Math.floor((lo + hi) / 2)

3. Conquer left

Solve the left half as its own smaller problem.

const left = maxValue(arr, lo, mid)

4. Conquer right

Solve the right half independently of the left.

const right = maxValue(arr, mid + 1, hi)

5. Combine

Merge two sub-answers into the answer for the whole range.

return Math.max(left, right)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Divide & Conquer: Lesson & Practice · Typorithm | ApplyDjinn