## Pages

### Find Maximum and minimum in an array Tournament Method (Divide and Conquer)

Find the maximum and minimum of an array using a minimum number of comparisons.

For every problem we will have multiple solutions, everyone can have their own solution.

For this problem here we have three approaches to solve this problem -

Second Approach "Tournament Method (Divide and Conquer)"

Description -

Divide the array into two parts and compare the maximums and minimums of the two parts to get the maximum and the minimum of the whole array.

********Code********

```package javaoneworld.learndsa.problems;

public class JavaOneWorldDSA {
/* Class Pair is used to return two values from getMinMax() */
static class Pair {

int min;
int max;
}

static Pair getMinMax(int arr[], int low, int high) {
Pair minmax = new Pair();
Pair mml = new Pair();
Pair mmr = new Pair();
int mid;

// If there is only one element
if (low == high) {
minmax.max = arr[low];
minmax.min = arr[low];
return minmax;
}

/* If there are two elements */
if (high == low + 1) {
if (arr[low] > arr[high]) {
minmax.max = arr[low];
minmax.min = arr[high];
} else {
minmax.max = arr[high];
minmax.min = arr[low];
}
return minmax;
}

/* If there are more than 2 elements */
mid = (low + high) / 2;
mml = getMinMax(arr, low, mid);
mmr = getMinMax(arr, mid + 1, high);

/* compare minimums of two parts*/
if (mml.min < mmr.min) {
minmax.min = mml.min;
} else {
minmax.min = mmr.min;
}

/* compare maximums of two parts*/
if (mml.max > mmr.max) {
minmax.max = mml.max;
} else {
minmax.max = mmr.max;
}

return minmax;
}

/* Testing the implemented program  */
public static void main(String args[]) {
int arr[] = {999, 98, 986, 56, 550, 7986};
int arr_size = 6;
Pair minMax = getMinMax(arr, 0, arr_size - 1);
System.out.println("Minimum element is :"+ minMax.min);
System.out.println("Maximum element is :"+ minMax.max);

}

}
```

Time Complexity - O(n)

Thanks a bunch for being here.
#stayhealthy
#takeCare

*************
******************
*************************