max min reference

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
822 B
4
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
void MaxMin(int arr[],int l,int h,int *max,int *min)
{
	int mid,max1,min1;
	if(l==h)
	{
		*max=*min=arr[l];
	}
	else if(h-l==1)
	{
		if(arr[l]<arr[h])
		{
				*max=arr[h];
				*min=arr[l];
		}
		else
		{
			*max=arr[l];
			*min=arr[h];
		}
	}
	else
	{
		mid=(l+h)/2;
		
		MaxMin(arr,l,mid,max,min);
		max1=*max;
		min1=*min;
		MaxMin(arr,mid+1,h,max,min);
		if(max1>*max)
		{
			*max=max1;
		}
		if(min1<*min)
			*min=min1;
		
	}
	
}
int main()
{
	int *arr,i,n,max,min;
	printf("\nEnter size of the array: ");
	scanf("%d",&n);
	arr=(int*)malloc(sizeof(int)*n);
	printf("\nEnter elements:");
	for(i=0;i<n;i++)
		scanf("%d",arr+i);
	//max=min=arr[0];
	MaxMin(arr,0,n-1,&max,&min);
	printf("\nMax= %d\nMin= %d",max,min);
	return 0;
}