GFG App
Open App
Browser
Continue

# C++ Program to Find Largest Element in an Array

Given an array arr of size N, the task is to find the largest element in the given array.

Example:

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

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

Approach 1 – Linear Traversal: One of the most simplest and basic approach to solve this problem is to simply traverse the whole list and find the maximum among them.

Follow the steps below to implement this idea:

• Create a local variable max to store the maximum among the list
• Initialize max with the first element initially, to start the comparison.
• Then traverse the given array from second element till end, and for each element:
• Compare the current element with max
• If the current element is greater than max, then replace the value of max with the current element.
• At the end, return and print the value of the largest element of array stored in max.

Below is the implementation of the above approach:

## CPP

 `// 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`

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.

Approach 2: Using Library Function: Most of the languages have a relevant max() type in-built function to find the maximum element, such as  std::max_element in C++. We can use this function to directly find the maximum element.

Below is the implementation of the above approach:

## 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);` `}`   `// Driver code` `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: O(N), since the in-built max_element() function takes O(N) time.
Auxiliary Space: O(1), as only an extra variable is created, which will take O(1) space.

Approach 3: Using Recursion: we will call recursive function for finding maximum elements in index  from 1 to n-1 and then call recursive function for finding maximum elements in index from 2 to n-1 and so on and return maximum element every time.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach`   `#include ` `using` `namespace` `std;`   `// Function to largest element using Recursion` `int` `largest(``int` `*arr , ``int` `n , ``int` `i)` `{` `    ``//Base case` `    ``if` `(i == n-1) ``// if we reach last element of the array      ` `    ``{             ``// return value of that index` `        ``return` `arr[i];` `    ``} `   `    ``// recursive call for find maximum element ` `    ``return` `max( arr[i] ,largest( arr , n , i+1) );` `    ``// it will return maximum of arr[i] and arr[i+1] to arr[n-1]` `}`   `// Drive Code` `int` `main()` `{` `    ``int` `arr[] = {10, 324, 45, 90, 9808};` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]);` `    `  `    ``// Function call` `    ``cout << largest(arr, n , 0);` `    `  `    ``return` `0;` `}`   `// This Approach is contributed by nikhilsainiofficial546`

Output

`9808`

Time complexity: O(N)
Auxiliary Space: O(N ), because of recursive call

Approach 4(Using sort funciton):

This approach to solve the problem is to sort the array in increasing order using sort stl and then return the last element of the array which will be the largest.

Algorithm:

1.    Define a function named largest which takes an integer array arr and its size n as input.
2.    Sort the array arr in increasing order using the sort function in C++.
3.    Return the (n-1)th element of the sorted array which will be the largest element in the array.
4.    In the main function, define an integer array arr and its size n, and assign some values to the array.
5.    Call the largest function with the array arr and size n as inputs and print its return value.

Below is the implementation of the approach:

## C++

 `// C++ implementation of the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find largest element ` `int` `largest(``int` `*arr, ``int` `n) {` `    ``// Sort the array in` `      ``// increasing order` `    ``sort(arr, arr + n);` `  `  `      ``// return the (n-1)th element` `      ``return` `arr[n-1];` `}`   `// Driver's Code` `int` `main() {` `    ``int` `arr[] = {10, 324, 45, 90, 9808};` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]);` `    `  `    ``// Function call` `    ``cout << largest(arr, n);` `    `  `    ``return` `0;` `}`

Output

`9808`

Time Complexity: O(N * log2N) as sort function has been called which takes N*log2N time. Here, N is size of input array.

Auxiliary Space: O(1) as no extra space has been used.

My Personal Notes arrow_drop_up