   Type like pro

# Find maximum and minimum

## Problem

Given an array of integers, find the maximum and minimum of the array.

## Constraint

Find the answer in minimum number of comparisons.

## Brute force

We can keep two variables named max and min. We can iterate over the list and compare each number with the min and the max, if the number is greater than the max update max, if the number is less than the min, update the min. In this brute force solution the number of comparison is 2*n.

## Better solution

If rather than comparing each number with max and min, we can first compare the numbers in pair with each other. Then compare the larger number with max and compare the smaller number with min. in this way the number of comparison for a pair of numbers are 3. So number of comparisons are 1.5 *n.

## Further discussion

Now let's think what will happen if instead of two numbers we take three numbers and then first find the largest and smallest of them and then compare these two numbers with max and min. To find the largest and smallest among 3 numbers, we need 3 comparisons in worst case and 2 comparisons in best case. The average case is 2.5. So for 3 numbers we need total 5 comparisons in worst case and 4.5 in average case. So in worst case comparisons per number is 1.6 and average case 1.5. Similarly we can derive that the worst case comparison is never less than 1.5. and best and average case is equal in case of taking numbers in pair.

## Divide and conquer method

In this approach we are dividing the list in two parts, each part is recursively providing the min and max of that part and then two min max are compared to decide the ultimate min and max. Recursively when the array is reduced to only a single element then the element itself become min and max.