# Program to find largest element in an array

## Program to find largest element in an array

Given an array, find the largest element in it.
Example:

```Input : arr[] = {10, 20, 4}
Output : 20

Input : arr[] = {20, 10, 20, 4, 100}
Output : 100```

The solution is to initialize max as first element, then traverse the given array from second element till end. For every traversed element, compare it with max, if it is greater than max, then update max.

 `// C++ program to find maximum` `// in arr[] of size n` `#include ` `using` `namespace` `std;` `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]);` `    ``cout << ``"Largest in given array is "` `         ``<< largest(arr, n);` `    ``return` `0;` `}` `// This Code is contributed` `// by Shivi_Aggarwal`

Output:

`Largest in given array is 9808`

Using Library Function :
We use std::max_element in C++.

 `// C++ program to find maximum in arr[] of size n` `#include ` `using` `namespace` `std;` `// returns maximum in arr[] of size n` `int` `largest(``int` `arr[], ``int` `n)` `{` `    ``return` `*max_element(arr, arr+n);` `}` `int` `main()` `{` `    ``int` `arr[] = {10, 324, 45, 90, 9808};` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]);` `    ``cout << largest(arr, n);` `    ``return` `0;` `}`

Output :

`9808`

Time complexity of the above solution is .

Last Updated on October 16, 2021 by admin

## Arrays in C/C++Arrays in C/C++

Arrays in C/C++ An array in C/C++ or be it in any programming language is

## Given an array A[] and a number x, check for pair in A[] with sum as xGiven an array A[] and a number x, check for pair in A[] with sum as x

Given an array A[] and a number x, check for pair in A[] with sum

## Write a program to reverse an array or stringWrite a program to reverse an array or string

Program to reverse an array or string Given an array (or string), the task is

## Sort an array of 0s, 1s and 2sSort an array of 0s, 1s and 2s

Sort an array of 0s, 1s and 2s Given an array A[] consisting 0s, 1s

## Array of Strings in C++Array of Strings in C++

Array of Strings in C++ (All Different Ways to Create) In C and C++, a

## Maximum and minimum of an array using minimum number of comparisonsMaximum and minimum of an array using minimum number of comparisons

Maximum and minimum of an array using minimum number of comparisons   Write a C

## Linear SearchLinear Search

Linear Search Problem: Given an array arr[] of n elements, write a function to search

## K’th Smallest/Largest Element in Unsorted ArrayK’th Smallest/Largest Element in Unsorted Array

K’th Smallest/Largest Element in Unsorted Array   Given an array and a number k where