# Implementing MinMax Algorithm in Java

```import java.io.*;
public class MinMax
{
static int mid;

public static void main(String args[])throws IOException
{

int ch;
int e[]= new int [2];
BufferedReader o=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no. element ");
int n=Integer.parseInt(o.readLine());
int a[]=new int[n];
for(int i=0;i<=n-1;i++)
{
System.out.println("Enter the element ");
a[i]=Integer.parseInt(o.readLine());
}
e=Minmax(a,0,n-1);
System.out.println("\nMinimum="+e[0]+"\nMaximum="+e[1]);

}

public static int[] Minmax(int a[],int low,int high)
{
int b[]=new int [2];     //b[0]=min,b[1]=max

if (low==high)
{
b[0]= a[low];
b[1]= a[low];
}
else
if (high-low==1)
{
if (a[high]<a[low])
{
b[0]=a[high];
b[1]=a[low];
}
else
{
b[0]=a[low];
b[1]=a[high];
}

}
else
{
mid=(low+high)/2;
int c[]=new int[2];
int d[]=new int[2];
c=Minmax(a,low,mid);
d=Minmax(a,mid+1,high);
b[0]=Min(c[0],d[0]);
b[1]=Max(c[1],d[1]);
}
return b;

}
public static int Min(int x1,int x2)
{
return(x1<x2?x1:x2);

}
public static int Max(int y1,int y2)
{
return(y1>y2?y1:y2);

}
}
/*      Output

Enter the no. element
5
Enter the element
0
Enter the element
1
Enter the element
2
Enter the element
3
Enter the element
5
Minimum=0
Maximum=5
Process Exit...

*/
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.