class Minmax
{
static int a[];
static int[] findmax(int[] b,int low,int high)
{
int mid,min,max,x,y;
int []temp1=new int[2];
int []temp2=new int[2];
if (low==high)
{
temp1[0]=b[low];
temp1[1]=b[high];
}
else if (high-low==1)
{
if (b[low]
{
temp1[0]=b[low];
temp1[1]=b[high];
}
else
{
temp1[0]=b[high];
temp1[1]=b[low];
}
}
else
{
mid=(low+high)/2;
temp1=findmax(b,low,mid);
temp2=findmax(b,mid+1,high);
x=min(temp1[0],temp2[0]);
y=max(temp1[1],temp2[1]);
temp1[0]=x;
temp1[1]=y;
}
return temp1;
}
static int min(int x,int y)
{
if(x
return x;
else
return y;
}
static int max(int x,int y)
{
if(x
return y;
else
return x;
}
public static void main(String args[]) throws IOException
{
Minmax m=new Minmax();
int[] sol=new int[2];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no of elements:");
int n=Integer.parseInt(br.readLine());
a=new int[n];
System.out.println("Enter the elements of array:");
for(int i=0;i
{
a[i]=Integer.parseInt(br.readLine());
}
sol=findmax(a,0,a.length-1);
System.out.println("min element:"+sol[0]);
System.out.println("max element:"+sol[1]);
}
}
/*Output:
Enter no of elements:
7
Enter the elements of array:
25 100 63 7 96 78 49
min element:7
max element:100
Process completed. */
0 comments:
Post a Comment