One of the most simplest and basic approaches to solving 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.
In the end, return and print the value of the largest element of array stored in max.
Below is the implementation of the above approach:
C++
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
usingnamespacestd;
intlargest(intarr[], intn)
{
inti;
// Initialize maximum element
intmax = 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];
returnmax;
}
// Driver Code
intmain()
{
intarr[] = {10, 324, 45, 90, 9808};
intn = sizeof(arr) / sizeof(arr[0]);
cout << "Largest in given array is "
<< largest(arr, n);
return0;
}
// This Code is contributed
// by Shivi_Aggarwal
C
// C program to find maximum in arr[] of size n
#include <stdio.h>
// C function to find maximum in arr[] of size n
intlargest(intarr[], intn)
{
inti;
// Initialize maximum element
intmax = 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];
returnmax;
}
intmain()
{
intarr[] = {10, 324, 45, 90, 9808};
intn = sizeof(arr)/sizeof(arr[0]);
printf("Largest in given array is %d", largest(arr, n));
return0;
}
Java
// Java Program to find maximum in arr[]
importjava.io.*;
classTest {
staticintarr[] = { 10, 324, 45, 90, 9808};
// Method to find maximum in arr[]
staticintlargest()
{
inti;
// Initialize maximum element
intmax = arr[0];
// Traverse array elements from second and
// compare every element with current max
for(i = 1; i < arr.length; i++)
if(arr[i] > max)
max = arr[i];
returnmax;
}
// Driver method
publicstaticvoidmain(String[] args)
{
System.out.println("Largest in given array is "
+ largest());
}
}
Python3
# Python3 program to find maximum
# in arr[] of size n
# Function to find maximum
# in arr[] of size n
deflargest(arr,n):
# Initialize maximum element
mx =arr[0]
# Traverse array elements from second
# and compare every element with
# current max
fori inrange(1, n):
ifarr[i] > mx:
mx =arr[i]
returnmx
# Driver Code
arr =[10, 324, 45, 90, 9808]
n =len(arr)
#calculating length of an array
Ans =largest(arr,n)
# display max
print("Largest in given array is",Ans)
# This code is contributed by Jai Parkash Bhardwaj
# and improved by prophet1999
C#
// C# Program to find maximum in arr[]
usingSystem;
classGFG {
staticint[]arr = {10, 324, 45, 90, 9808};
// Method to find maximum in arr[]
staticintlargest()
{
inti;
// Initialize maximum element
intmax = arr[0];
// Traverse array elements from second and
// compare every element with current max
for(i = 1; i < arr.Length; i++)
if(arr[i] > max)
max = arr[i];
returnmax;
}
// Driver method
publicstaticvoidMain()
{
Console.WriteLine("Largest in given "
+ "array is "+ largest());
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to find maximum
// in arr[] of size n
// PHP function to find maximum
// in arr[] of size n
functionlargest($arr, $n)
{
$i;
// Initialize maximum element
$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
$arr= array(10, 324, 45, 90, 9808);
$n= sizeof($arr);
echo"Largest in given array is "
, largest($arr, $n);
// This code is contributed by aj_36
?>
Javascript
<script>
// JavaScript program to find
// maximum in arr[] of size n
functionlargest(arr) {
let i;
// Initialize maximum element
let max = arr[0];
// Traverse array elements
// from second and compare
// every element with current max
for(i = 1; i < arr.length; i++) {
if(arr[i] > max)
max = arr[i];
}
returnmax;
}
// Driver code
let arr = [10, 324, 45, 90, 9808];
document.write("Largest in given array is "+ largest(arr));
// This code is contributed by Surbhi Tyagi
</script>
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 (Recursive Solution):The idea is to traverse over the array recursively and find the maximum element.
Below is the code for the mentioned approach:
C++
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
usingnamespacestd;
intlargest(intarr[], intn, inti)
{
// last index
// return the element
if(i == n - 1) {
returnarr[i];
}
// find the maximum from rest of the array
intrecMax = largest(arr, n, i + 1);
// compare with i-th element and return
returnmax(recMax, arr[i]);
}
// Driver Code
intmain()
{
intarr[] = { 10, 324, 45, 90, 9808 };
intn = sizeof(arr) / sizeof(arr[0]);
cout << "Largest in given array is "
<< largest(arr, n, 0);
return0;
}
// This Code is contributed by Rajdeep Mallick
Java
// Java program to find maximum
// in arr[] of size n
importjava.io.*;
classGFG {
staticintlargest(intarr[], intn, inti)
{
// last index
// return the element
if(i == n - 1) {
returnarr[i];
}
// find the maximum from rest of the array
intrecMax = largest(arr, n, i + 1);
// compare with i-th element and return
returnMath.max(recMax, arr[i]);
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intarr[] = { 10, 324, 45, 90, 9808};
intn = arr.length;
System.out.println("Largest in given array is "
+ largest(arr, n, 0));
}
}
// this code is contributed by rajdeep999
Python3
importmath
classGFG :
@staticmethod
deflargest( arr, n, i) :
# last index
# return the element
if(i ==n -1) :
returnarr[i]
# find the maximum from rest of the array
recMax =GFG.largest(arr, n, i +1)
# compare with i-th element and return
returnmax(recMax,arr[i])
# Driver Code
@staticmethod
defmain( args) :
arr =[10, 324, 45, 90, 9808]
n =len(arr)
print("Largest in given array is "+str(GFG.largest(arr, n, 0)))
if__name__=="__main__":
GFG.main([])
# This code is contributed by aadityaburujwale.
C#
// Include namespace system
usingSystem;
publicclassGFG
{
publicstaticintlargest(int[] arr, intn, inti)
{
// last index
// return the element
if(i == n - 1)
{
returnarr[i];
}
// find the maximum from rest of the array
varrecMax = GFG.largest(arr, n, i + 1);
// compare with i-th element and return
returnMath.Max(recMax,arr[i]);
}
// Driver Code
publicstaticvoidMain(String[] args)
{
int[] arr = {10, 324, 45, 90, 9808};
varn = arr.Length;
Console.WriteLine("Largest in given array is "+ GFG.largest(arr, n, 0).ToString());
}
}
// This code is contributed by aadityaburujwale.
Javascript
// JS program to find maximum
// in arr[] of size n
functionlargest(arr, n, i)
{
// last index
// return the element
if(i == n - 1) {
returnarr[i];
}
// find the maximum from rest of the array
let recMax = largest(arr, n, i + 1);
// compare with i-th element and return
returnMath.max(recMax, arr[i]);
}
// Driver Code
let arr = [ 10, 324, 45, 90, 9808 ];
let n = arr.length;
console.log("Largest in given array is ", largest(arr, n, 0));
// This Code is contributed by akashish__
Output
Largest 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
Approach 3: 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 <bits/stdc++.h>
usingnamespacestd;
// returns maximum in arr[] of size n
intlargest(intarr[], intn)
{
return*max_element(arr, arr+n);
}
intmain()
{
intarr[] = {10, 324, 45, 90, 9808};
intn = sizeof(arr)/sizeof(arr[0]);
cout << largest(arr, n);
return0;
}
Java
// Java program to
// find maximum in
// arr[] of size n
importjava .io.*;
importjava.util.*;
classGFG
{
// returns maximum in
// arr[] of size n
staticintlargest(int[]arr,
intn)
{
Arrays.sort(arr);
returnarr[n - 1];
}
// Driver code
staticpublicvoidmain (String[] args)
{
int[]arr = {10, 324, 45,
90, 9808};
intn = arr.length;
System.out.println(largest(arr, n));
}
}
// This code is contributed
// by anuj_67.
Python3
# Python 3 program to find
# maximum in arr[] of size n
# returns maximum
# in arr[] of size n
deflargest(arr, n):
returnmax(arr)
# driver code
arr =[10, 324, 45, 90, 9808]
n =len(arr)
print(largest(arr, n))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to find maximum in
// arr[] of size n
usingSystem;
usingSystem.Linq;
publicclassGFG {
// returns maximum in arr[]
staticintLargest(int[] arr)
{
returnarr.Max();
}
// Driver code
staticpublicvoidMain ()
{
int[] arr = {10, 324, 45, 90, 9808};
Console.WriteLine(Largest(arr));
}
}
// This code is contributed by anuj_67.
// Minor code cleanup by shanmukha7
PHP
<?php
// PHP program to find maximum
// in arr[] of size n
// returns maximum in
// arr[] of size n
functionlargest( $arr, $n)
{
returnmax($arr);
}
// Driver COde
$arr= array(10, 324, 45, 90, 9808);
$n= count($arr);
echolargest($arr, $n);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// Javascript program to find maximum in arr[] of size n
// returns maximum in arr[] of size n
functionlargest(arr, n)
{
arr.sort();
returnarr[n-1];
}
let arr = [10, 324, 45, 90, 9808];
let n = arr.length;
document.write(largest(arr, n));
//This code is contributed by Mayank Tyagi
</script>
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Please Login to comment...