C Program to Find Maximum Element in an Array

  • Write a C program to find largest element of an array.

Given an array of length N, we have to find the maximum element in array and it’s position in array. We can find the maximum element in an array by traversing the array from index 0 to N-1 and comparing each element with the maximum element found till now using comparison operators.
Algorithm to find maximum element of array

  • First of all, take N numbers as input from user and store it in an array(lets call it inputArray).
  • We will declare variable maxElement and maxElementPosition and initialize it with first element of inputArray and 0 respectively.
  • We will start traversing inputArray from index 0 to N -1 and compare each element with maxElement. If maxElement is less than current element, we will update maxElement and maxElementPosition with current element and current position respectively.
  • At any instant of time suppose we are at index i, then maxElement will give the maximum element between array index 0 to i.

Time Complexity : O(n)

C program to find maximum element of array

In below program, we first take number of elements in array as input from user and stores in an integer variable ‘elementCount’. Then we take ‘elementCount’ numbers as input from user and stores them in an integer array using a for loop. Now, we assume that first element(inputArray[0]) is the maximum element of inputArray and starts traversing inputArray form index 0 to N-1. For every element we compare it’s value with maxElement and update value of maxElement If current element is greater than max element. At the end of traversal, maxElement will contain the maximum element of inputArray and maxElementPosition will contain it’s position in array.

C Program to Find Maximum Element in an Array

/*
* C Program to find maximum element in an array
*/
 
#include <stdio.h>
#include <conio.h>
 
int main(){
    int maxElement, inputArray[500], maxElementPosition;
    int elementCount, counter;
     
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
     
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    /* Initializing maxElement and maxElementPosition 
     * with first element of inputArray and zero 
     * respectively
     */
    maxElement = inputArray[0];
    maxElementPosition = 0;
     
    /* Compare each element of input array with maxElement, and
       if it is greater than maxElement update maxElement and 
       maxElementPosition
    */
    for(counter = 1; counter < elementCount; counter++){
        if(inputArray[counter] > maxElement){
            maxElement = inputArray[counter];
            maxElementPosition = counter;
        }
    }
    /* Print Maximum element and it's position in array */
    printf("Maximum element in array is %d at index %d",
        maxElement, maxElementPosition);
     
    getch();
    return 0;
}

Program Output

Enter number of elements in array: 5
Enter 5 numbers
3 0 9 6 1
Maximum element in array is 9 at index 2

C Program to find maximum elements in an array using divide and conquer

Below program divides this problem into two sub-problems by splitting input array into two equal half. Then it calculates the maximum elements of both sub array by recursively calling itself for left and right sub-array.

To find the maximum element of whole array it takes maximum of leftMax and rightMax.

C Program to find maximum elements in an array using divide and conquer

/*
* C Program to find maximum element in an array
*/
#include <stdio.h>
#include <conio.h>
 
int getMax(int num1, int num2);
int getMaxElement(int *array, int leftIndex, int rightIndex);
 
int main(){
    int maxElement, inputArray[500];
    int elementCount, counter;
     
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
     
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    maxElement = getMaxElement(inputArray, 0, elementCount-1);
    printf("Maximum element in array is %d ", maxElement);
     
    getch();
    return 0;
}
 
/*
* Function to find max of two number 
*/
int getMax(int num1, int num2){
    if(num1 >= num2)
        return num1;
    else
        return num2;    
}
 
/*
*  Function to find maxElement of Array using Divide and Conquer
*/
int getMaxElement(int *array, int leftIndex, int rightIndex){
    int midIndex, leftMax, rightMax;
    if(NULL == array){
        printf("Invalid Input");
        return -1;
    }
    /*  exit condition, for sub-Array of size 1  */
    if(leftIndex == rightIndex)
        return array[leftIndex];
    /* Divide input array in two equal half 
     * find max Element of left and right half of Array
     * return maximum of leftMax and rightMax
     */
    midIndex = (leftIndex + rightIndex) / 2;
    leftMax = getMaxElement(array, leftIndex, midIndex);
    rightMax = getMaxElement(array, midIndex+1, rightIndex);
     
    return getMax(leftMax, rightMax);
}

Program Output

Enter number of elements in array: 6
Enter 6 numbers
7 2 5 1 1 9
Maximum element in array is 9