DryRun
AlgorithmsCourseCompare
DryRun
AlgorithmsCourseCompareGitHubLinkedIn

© 2026 DryRun. Built by Aditya Pandey.

AlgorithmsAdvancedMax and Min (Divide and Conquer)
Advancedbeginnerdivide-and-conquerarrayrecursion

Max and Min (Divide and Conquer)

Split array into halves, compute (min, max) for each half, then combine by comparing local minima and maxima. This reduces comparison count versus naïve independent scans for min and max.

Complexity

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(log n)

Implementation

pair<int,int> minMax(vector<int>& a, int l, int r) {
    if (l == r) return {a[l], a[l]};
    if (r == l + 1) return {min(a[l], a[r]), max(a[l], a[r])};
    int m = (l + r) / 2;
    auto L = minMax(a, l, m);
    auto R = minMax(a, m + 1, r);
    return {min(L.first, R.first), max(L.second, R.second)};
}

How It Works

1.Split

Break into two halves until base cases of one or two elements.

2.Combine

Merge by min(min1, min2) and max(max1, max2).

3.Comparison Count

Uses fewer comparisons than finding min and max independently.

Related Algorithms

SearchBinary SearchArrowUpDownMerge Sort

Test Your Knowledge

Quiz coming soon for this algorithm.

Back to all algorithms