# 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];
System.out.println("Enter the no. element ");
int a[]=new int[n];
for(int i=0;i<=n-1;i++)
{
System.out.println("Enter the element ");
}
e=Minmax(a,0,n-1);
System.out.println("\nMinimum="+e&#91;0&#93;+"\nMaximum="+e&#91;1&#93;);

}

public static int&#91;&#93; Minmax(int a&#91;&#93;,int low,int high)
{
int b&#91;&#93;=new int &#91;2&#93;;     //b&#91;0&#93;=min,b&#91;1&#93;=max

if (low==high)
{
b&#91;0&#93;= a&#91;low&#93;;
b&#91;1&#93;= a&#91;low&#93;;
}
else
if (high-low==1)
{
if (a&#91;high&#93;<a&#91;low&#93;)
{
b&#91;0&#93;=a&#91;high&#93;;
b&#91;1&#93;=a&#91;low&#93;;
}
else
{
b&#91;0&#93;=a&#91;low&#93;;
b&#91;1&#93;=a&#91;high&#93;;
}

}
else
{
mid=(low+high)/2;
int c&#91;&#93;=new int&#91;2&#93;;
int d&#91;&#93;=new int&#91;2&#93;;
c=Minmax(a,low,mid);
d=Minmax(a,mid+1,high);
b&#91;0&#93;=Min(c&#91;0&#93;,d&#91;0&#93;);
b&#91;1&#93;=Max(c&#91;1&#93;,d&#91;1&#93;);
}
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.