import
java.io.*;
import
java.util.*;
class
MinMax{
static
int minimax(int depth, int nodeIndex, boolean isMax,
int
scores[], int h)
{
if
(depth == h)
return
scores[nodeIndex];
if
(isMax)
return
Math.max(minimax(depth+1, nodeIndex*2, false, scores, h),
minimax(depth+1,
nodeIndex*2 + 1, false, scores, h));
else
return
Math.min(minimax(depth+1, nodeIndex*2, true, scores, h),
minimax(depth+1,
nodeIndex*2 + 1, true, scores, h));
}
static
int log2(int n)
{
return
(n==1)? 0 : 1 + log2(n/2);
}
public
static void main (String[] args) {
Scanner
sc=new Scanner(System.in);
System.out.println("Enter
Length of Element :");
int n = sc.nextInt();
int
scores[] = new int[n];
System.out.println("Enter
Element :");
for(int
i=0;i<n;i++){
scores[i]=sc.nextInt();
}
int
h = log2(n);
int
res = minimax(0, 0, true, scores, h);
System.out.println(
"The optimal value is : " +res);
}
}
Output:
Enter
Length of Element :
10
Enter
Element :
7
8
9
4
5
6
3
1
2
0
The
optimal value is : 8
No comments:
Post a Comment