C program to find the maximum and minimum element of the array

Given an array arr[] of N integers, the task is to write the C program to find the maximum and minimum element of the given array iteratively and recursively.

Examples:

Input: arr[] = {1, 2, 4, -1}
Output:
The minimum element is -1
The maximum element is 4

Input: arr[] = {-1, -1, -1, -1}
Output:
The minimum element is -1
The maximum element is -1

Approach:



  1. Let maxE and minE be the variable to store the minimum and maximum element of the array.
  2. Initialise minE as INT_MAX and maxE as INT_MIN.
  3. Traverse the given array arr[].
  4. If the current element is smaller than minE, then update the minE as current element.
  5. If the current element is greater than maxE, then update the maxE as current element.
  6. Repeat the above two steps for the element in the array.

Below is the implementation of the above approach:

Iterative Approach

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for the above approach
#include <limits.h>
#include <stdio.h>
  
// Function to find the minimum and
// maximum element of the array
void findMinimumMaximum(int arr[], int N)
{
    int i;
  
    // variable to store the minimum
    // and maximum element
    int minE = INT_MAX, maxE = INT_MIN;
  
    // Traverse the given array
    for (i = 0; i < N; i++) {
  
        // If current element is smaller
        // than minE then update it
        if (arr[i] < minE) {
            minE = arr[i];
        }
  
        // If current element is greater
        // than maxE then update it
        if (arr[i] > maxE) {
            maxE = arr[i];
        }
    }
  
    // Print the minimum and maximum element
    printf("The minimum element is %d", minE);
    printf("\n");
    printf("The maximum element is %d", maxE);
  
    return;
}
  
// Driver Code
int main()
{
  
    // Given array
    int arr[] = { 1, 2, 4, -1 };
  
    // length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    findMinimumMaximum(arr, N);
  
    return 0;
}

chevron_right


Recursive Approach

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for the above approach
#include <limits.h>
#include <stdio.h>
  
// Recursive function to find the minimum
// and the maximum element of the array
void recursiveMinMax(int arr[], int N,
                     int* minE, int* maxE)
{
    // Base Case
    if (N < 0) {
        return;
    }
  
    // If current element is smaller
    // than minE then update it
    if (arr[N] < *minE) {
        *minE = arr[N];
    }
  
    // If current element is greater
    // than maxE then update it
    if (arr[N] > *maxE) {
        *maxE = arr[N];
    }
  
    // Recursive call for next iteration
    recursiveMinMax(arr, N - 1, minE, maxE);
}
  
// Function to find the minimum and
// maximum element of the array
void findMinimumMaximum(int arr[], int N)
{
    int i;
  
    // variable to store the minimum
    // and maximum element
    int minE = INT_MAX, maxE = INT_MIN;
  
    // Recursive Function to find
    // minimum & maximum element
    recursiveMinMax(arr, N - 1, &minE, &maxE);
  
    // Print the minimum and maximum element
    printf("The minimum element is %d", minE);
    printf("\n");
    printf("The maximum element is %d", maxE);
  
    return;
}
  
// Driver Code
int main()
{
  
    // Given array
    int arr[] = { 1, 2, 4, -1 };
  
    // length of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    findMinimumMaximum(arr, N);
    return 0;
}

chevron_right


Output:

The minimum element is -1
The maximum element is 4

Time Complexity: O(N), where N is the number of element in the given array.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.