Given an array arr[] of size N, the task is to find the largest element in the given array.
Examples:
Input: arr[] = {10, 20, 4}
Output: 20
Explanation: Among 10, 20 and 4, 20 is the largest.
Input : arr[] = {20, 10, 20, 4, 100}
Output : 100
Iterative Approach to find the largest element of Array:
The simplest approach is to solve this problem is to traverse the whole list and find the maximum among them.
- Create a local variable max and initiate it to arr[0] to store the maximum among the list
- Iterate over the array
- Compare arr[i] with max.
- If arr[i] > max, update max = arr[i].
- Increment i once.
- After the iteration is over, return max as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largest( int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
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;
}
|
C
#include <stdio.h>
int largest( int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
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;
}
|
Java
import java.io.*;
class Test {
static int arr[] = { 10 , 324 , 45 , 90 , 9808 };
static int largest()
{
int i;
int max = arr[ 0 ];
for (i = 1 ; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void main(String[] args)
{
System.out.println( "Largest in given array is "
+ largest());
}
}
|
Python3
def largest(arr, n):
mx = arr[ 0 ]
for i in range ( 1 , n):
if arr[i] > mx:
mx = arr[i]
return mx
if __name__ = = '__main__' :
arr = [ 10 , 324 , 45 , 90 , 9808 ]
n = len (arr)
Ans = largest(arr, n)
print ( "Largest in given array is" , Ans)
|
C#
using System;
class GFG {
static int [] arr = { 10, 324, 45, 90, 9808 };
static int largest()
{
int i;
int max = arr[0];
for (i = 1; i < arr.Length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void Main()
{
Console.WriteLine( "Largest in given "
+ "array is " + largest());
}
}
|
Javascript
<script>
function largest(arr) {
let i;
let max = arr[0];
for (i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
let arr = [10, 324, 45, 90, 9808];
document.write( "Largest in given array is " + largest(arr));
</script>
|
PHP
<?php
function largest( $arr , $n )
{
$i ;
$max = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] > $max )
$max = $arr [ $i ];
return $max ;
}
$arr = array (10, 324, 45, 90, 9808);
$n = sizeof( $arr );
echo "Largest in given array is "
, largest( $arr , $n );
?>
|
OutputLargest 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.
Recursive Approach to find the maximum of Array:
The idea is similar to the iterative approach. Here the traversal of the array is done recursively instead of an iterative loop.
- Set an integer i = 0 to denote the current index being searched.
- Check if i is the last index, return arr[i].
- Increment i and call the recursive function for the new value of i.
- Compare the maximum value returned from the recursion function with arr[i].
- Return the max between these two from the current recursion call.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largest( int arr[], int n, int i)
{
if (i == n - 1) {
return arr[i];
}
int recMax = largest(arr, n, i + 1);
return max(recMax, arr[i]);
}
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, 0);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int largest( int arr[], int n, int i)
{
if (i == n - 1 ) {
return arr[i];
}
int recMax = largest(arr, n, i + 1 );
return Math.max(recMax, arr[i]);
}
public static void main(String[] args)
{
int arr[] = { 10 , 324 , 45 , 90 , 9808 };
int n = arr.length;
System.out.println( "Largest in given array is "
+ largest(arr, n, 0 ));
}
}
|
Python3
import math
def largest(arr, n, i):
if (i = = n - 1 ):
return arr[i]
recMax = largest(arr, n, i + 1 )
return max (recMax, arr[i])
if __name__ = = '__main__' :
arr = [ 10 , 324 , 45 , 90 , 9808 ]
n = len (arr)
print ( "Largest in given array is " + str (largest(arr, n, 0 )))
|
C#
using System;
public class GFG {
public static int largest( int [] arr, int n, int i)
{
if (i == n - 1) {
return arr[i];
}
var recMax = GFG.largest(arr, n, i + 1);
return Math.Max(recMax, arr[i]);
}
public static void Main(String[] args)
{
int [] arr = { 10, 324, 45, 90, 9808 };
var n = arr.Length;
Console.WriteLine(
"Largest in given array is "
+ GFG.largest(arr, n, 0).ToString());
}
}
|
Javascript
function largest(arr, n, i)
{
if (i == n - 1) {
return arr[i];
}
let recMax = largest(arr, n, i + 1);
return Math.max(recMax, arr[i]);
}
let arr = [ 10, 324, 45, 90, 9808 ];
let n = arr.length;
console.log( "Largest in given array is" , largest(arr, n, 0));
|
PHP
<?php
function largest( $arr , $n , $i )
{
if ( $i == $n - 1) {
return $arr [ $i ];
}
$recMax = largest( $arr , $n , $i + 1);
return max( $recMax , $arr [ $i ]);
}
$arr = [ 10, 324, 45, 90, 9808 ];
$n = count ( $arr );
echo "Largest in given array is " , largest( $arr , $n , 0) ;
?>
|
OutputLargest in given array is 9808
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for recursive calls
Find the maximum of Array 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++
#include <bits/stdc++.h>
using namespace std;
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int largest( int [] arr, int n)
{
Arrays.sort(arr);
return arr[n - 1 ];
}
static public void main(String[] args)
{
int [] arr = { 10 , 324 , 45 , 90 , 9808 };
int n = arr.length;
System.out.println(largest(arr, n));
}
}
|
Python3
def largest(arr, n):
return max (arr)
if __name__ = = '__main__' :
arr = [ 10 , 324 , 45 , 90 , 9808 ]
n = len (arr)
print (largest(arr, n))
|
C#
using System;
using System.Linq;
public class GFG {
static int Largest( int [] arr) {
return arr.Max();
}
static public void Main()
{
int [] arr = { 10, 324, 45, 90, 9808 };
Console.WriteLine(Largest(arr));
}
}
|
Javascript
<script>
function largest(arr, n)
{
arr.sort();
return arr[n-1];
}
let arr = [10, 324, 45, 90, 9808];
let n = arr.length;
document.write(largest(arr, n));
</script>
|
PHP
<?php
function largest( $arr , $n )
{
return max( $arr );
}
$arr = array (10, 324, 45, 90, 9808);
$n = count ( $arr );
echo largest( $arr , $n );
?>
|
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.
Refer below article for more methods: Program to find the minimum (or maximum) element of an array