Recursive Function for finding the largest and smallest of n distinctive positive integers
Let N[] be an array holding n distinctive positive integers.
Index - Current index of the array (initially zero)
Size - Size of the array (number of elements)
Set min = INT_MAX
Set max = INT_MIN
Find_min_max( N[], index, size):
min = N[index] < min ? N[index] : min;
curmax = N[index] > max ? N[index] : max;
If curmax != min :
max = curmax;
If index < size :
Find_min_max ( N,index+1, size);
Else
Return;
Recurrence Relation :
T(n) = T(n-1) + O(1)
Run time :
O(n)
C Implementation:
Let N[] be an array holding n distinctive positive integers.
Index - Current index of the array (initially zero)
Size - Size of the array (number of elements)
Set min = INT_MAX
Set max = INT_MIN
Find_min_max( N[], index, size):
min = N[index] < min ? N[index] : min;
curmax = N[index] > max ? N[index] : max;
If curmax != min :
max = curmax;
If index < size :
Find_min_max ( N,index+1, size);
Else
Return;
Recurrence Relation :
T(n) = T(n-1) + O(1)
Run time :
O(n)
C Implementation:
#include <stdio.h>
#include <limits.h>
void find_min_max(int *a,int index,int size,int *min,int *max)
{
 int curmax;
 *min = a[index] < *min ? a[index] : *min;
 curmax = a[index] > *max ? a[index] : *max;
 if(curmax != *min)
  *max = curmax;
 if(index < size)
  find_min_max(a,index+1,size,min,max);
 else
  return;
}
int main(void) {
 int n;
 int i;
 int a[1000];
 int max = INT_MIN;
 int min = INT_MAX;
 scanf("%d",&n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 find_min_max(a,0,n-1,&min,&max);
 printf("min = %d and max = %d",min,max);
 return 0;
}
Input:
6
-5 4 2 -9 2 3
Output:
min = -9 and max = 4