C program to find maximum and minimum elements in array using recursion

Write a C program to find maximum and minimum elements in a given array using recursion. How to find maximum and minimum elements in a given array using recursion in C programming. Using recursion find maximum or minimum elements in array in C program.

Example

Input

Size of array: 10
Elements in array: 5, 1, 6, 10, 2, 3, 6, 50, -7, 4

Output

Maximum element = 50
Minimum element = -7

Required knowledge

Basic C programming, Array, Function and Recursion

We have already done lot of exercises on array. In case you missed the simple iterative approach to find maximum and minimum element in an array, here it is.

Logic to find maximum and minimum elements using recursion

Recursion works on the concept of divide and conquer. We break the problem in its smallest size where it can be solved directly. For the maximum/minimum problem, the smallest problem size would be finding maximum/minimum between two elements.

Before writing our main logic, let us first define a recursive function to find maximum element in an array.

/**
 * @array Array in which maximum element is to be found
 * @index Current array element 
 * @N Size of the array
 */
int maximum(int array[], int index, int N);

For this recursive approach to find maximum, I have divided the algorithm in three main steps.

  1. If only two elements are left. We can easily find maximum using array[currentIndex] > array[currentIndex + 1].

    /*
     * This conditions confirms that only two elements 
     * are left i.e. array[index] and array[index + 1]
     */
    if(index == N-2)
    {
        if(array[index] > array[index + 1]) 
        {
            // array[index] i.e. second last element is max
        }
        else
        {
            // array[index + 1] i.e. last element is max
        }
    }
    
  2. Find maximum on the right side of current array index. For this step we will use recursive function call.

    max = maximum(array, index + 1, N);
    
  3. Finally compare the current array element with maximum element on its right (found in above step).

    if(array[index] > max)
    {
        // array[index] is maximum
    }
    else
    {
        // max is maximum
    }
    

Let us now implement these all steps in a program to find maximum and minimum.

Program to find maximum and minimum element using recursion

/**
 * C program to find maximum and minimum elements in array using recursion
 */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array


/* Function declarations */
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);




int main()
{
    int array[MAX_SIZE], N, max, min;
    int i;

    /* Reads size and elements of array */
    printf("Enter size of the array: ");
    scanf("%d", &N);
    printf("Enter %d elements in array: ", N);
    for(i=0; i<N; i++)
    {
        scanf("%d", &array[i]);
    }

    max = maximum(array, 0, N);
    min = minimum(array, 0, N);

    printf("Minimum element in array = %d\n", min);
    printf("Maximum element in array = %d\n", max);

    return 0;
}



/**
 * Recursive function to find maximum element in the given array.
 */
int maximum(int array[], int index, int len)
{
    int max;

    /*
     * Only last and second last element are left
     */
    if(index >= len-2)
    {
        if(array[index] > array[index + 1])
            return array[index];
        else
            return array[index + 1];
    }


    /*
     * Recursively call maximum to find maximum element in
     * right side of the array from current index.
     */
    max = maximum(array, index + 1, len);

    /*
     * Compare the current array element with maximum 
     * element on its right side
     */
    if(array[index] > max)
        return array[index];
    else
        return max;
}



/**
 * Recursive function to find minimum element in the array.
 */
int minimum(int array[], int index, int len)
{
    int min;

    if(index >= len-2)
    {
        if(array[index] < array[index + 1])
            return array[index];
        else
            return array[index + 1];
    }

    min = minimum(array, index + 1, len);

    if(array[index] < min)
        return array[index];
    else
        return min;
}

Note: You can also use conditional operators to write the same program in short.

Program to find maximum/minimum in array using recursion and conditional operator

/**
 * C program to find maximum and minimum elements in array using recursion
 */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array


/* Function declarations */
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);




int main()
{
    int array[MAX_SIZE], N, max, min;
    int i;

    /* Reads size and elements of array */
    printf("Enter size of the array: ");
    scanf("%d", &N);
    printf("Enter %d elements in array: ", N);
    for(i=0; i<N; i++)
    {
        scanf("%d", &array[i]);
    }

    max = maximum(array, 0, N);
    min = minimum(array, 0, N);

    printf("Minimum element in array = %d\n", min);
    printf("Maximum element in array = %d\n", max);

    return 0;
}



/**
 * Recursive function to find maximum element in the given array.
 */
int maximum(int array[], int index, int len)
{
    int max;

    if(index >= len-2)
        return (array[index] > array[index + 1])
                    ? array[index]
                    : array[index + 1];

    max = maximum(array, index + 1, len);

    return (array[index] > max)
                ? array[index]
                : max;
}



/**
 * Recursive function to find minimum element in the array.
 */
int minimum(int array[], int index, int len)
{
    int min;

    if(index >= len-2)
    {
        return (array[index] < array[index + 1])
                    ? array[index]
                    : array[index + 1];
    }

    min = minimum(array, index + 1, len);

    return (array[index] < min)
                ? array[index]
                : min;
}
Output
Enter size of the array: 10
Enter 10 elements in array: -1 0 5 4 3 10 999 -1 100 -100
Minimum element in array = -100
Maximum element in array = 999

Happy coding ;)

Recommended posts

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment