The Wayback Machine - https://web.archive.org/web/20240721052532/https://www.geeksforgeeks.org/largest-array-element-in-c/
Open In App

C Program to Find Largest Element in an Array

Last Updated : 06 Dec, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In this article, we will see how to find the largest element in the array using a C++ program.

The brute force approach to find the maximum element in an array is to compare each element with all other elements in the array. But we can optimize it by assuming an element as maximum and start traversing the array. If we find any element greater than the assumed max, we assume that element as the new max.

Algorithm

  1. Create a variable max to store the maximum element so far.
  2. Initialize max with the element on the first index of the array.
  3. Run a loop from the second index to traverse the array.
  4. Compare the variable max with the current element and check,
    1. If the current element is greater than max(max less than current element), update max with the current element.
  5. After the loop, variable max will hold the maximum element in the array.

largest element in the array in C++

C Program to Find the Largest Number in an Array

C




// C program to find maximum in
// arr[] of size n
#include <stdio.h>
 
// C function to find maximum
// in arr[] of size n
int largest(int arr[], int n)
{
    int i;
 
    // Initialize maximum element
    int max = arr[0];
 
    // Traverse array elements from
    // second and compare every
    // element with current max
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];
 
    return max;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 324, 45, 90, 9808 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Largest in given array is %d", largest(arr, n));
    return 0;
}


Output

Largest in given array is 9808


Complexity Analysis

  • Time complexity: O(N), to traverse the Array completely.
  • Auxiliary Space: O(1), as only an extra variable is created, which will take O(1) space.

Refer to the complete article Program to find largest element in an Array for optimized methods to find the largest element in an array.



Similar Reads

C Program to Find Largest Element in an Array using Pointers
Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanation:The largest element present in the given array
2 min read
C Program for Find largest prime factor of a number
Given a positive integer 'n'( 1 &lt;= n &lt;= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is '3' Input: 15 Output: 5Method 1: The approach is simple, just factorise the given number by dividing it with the divisor of a number and keep updating the maximum prime factor.
3 min read
C program to Find the Largest Number Among Three Numbers
Here, we will learn how to find the largest number among the three numbers using the C program. For example, the largest number among 2, 8, and 1 is 8. Methods to Find the Largest of Three NumbersUsing if StatementUsing if-else StatementUsing nested if-else Statement Let's discuss each of them. 1. Find the Largest Number Using if-else Statements Th
3 min read
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 A
3 min read
C / C++ Program for Largest Sum Contiguous Subarray
Write a C/C++ program for a given array arr[] of size N. The task is to find the sum of the contiguous subarray within a arr[] with the largest sum. Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. C / C++ Program for Largest Sum Contiguous Subarray using Kadane's Algorithm:The idea of Kadane’s algorithm is to ma
5 min read
C Program for Find the element that appears once
Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity is O(n) and O(1) extra space. Examples : Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3} Output: 2 In the given array all element appear three times except 2 which appears once. Input: arr[]
3 min read
C Program To Find Next Greater Element
Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1.  Examples:  For an array, the rightmost element always has the next greater element
5 min read
Largest gap in an array
Given an unsorted array of length N, and we have to find the largest gap between any two elements of the array. In simple words, find max(|Ai-Aj|) where 1 ? i ? N and 1 ? j ? N. Examples: Input : arr = {3, 10, 6, 7} Output : 7 Explanation : Here, we can see largest gap can be found between 3 and 10 which is 7 Input : arr = {-3, -1, 6, 7, 0} Output
9 min read
C program to Insert an element in an Array
Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C. Follow the below steps to solve the problem: First get the element to be inserted,
2 min read
C program to count frequency of each element in an array
Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array. Examples: Input: arr[] = { 1, 100000000, 3, 100000000, 3 } Output: { 1 : 1, 3 : 2, 100000000 : 2 } Explanation: Distinct elements of the given array are { 1, 100000000, 3 } Frequency of 1 in the given array is 1. Frequency of 10000
4 min read
three90RightbarBannerImg