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.
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++
// C++ program to find maximum in arr[] of size n #include <bits/stdc++.h> using namespace std; // Function to find the maximum of array 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]); // Function call cout << "Largest in given array is " << largest(arr, n); return 0; } // This Code is contributed by Shivi_Aggarwal |
C
// C program to find maximum in arr[] of size n #include <stdio.h> // 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]); // Function call printf ( "Largest in given array is %d" , largest(arr, n)); return 0; } |
Java
// Java Program to find maximum in arr[] import java.io.*; class Test { static int arr[] = { 10 , 324 , 45 , 90 , 9808 }; // Method to find maximum in arr[] static int largest() { 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 < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; } // Driver code public static void main(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 def largest(arr, n): # Initialize maximum element mx = arr[ 0 ] # Traverse array elements from second # and compare every element with # current max for i in range ( 1 , n): if arr[i] > mx: mx = arr[i] return mx # Driver Code if __name__ = = '__main__' : 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[] using System; class GFG { static int [] arr = { 10, 324, 45, 90, 9808 }; // Method to find maximum in arr[] static int largest() { 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 < arr.Length; i++) if (arr[i] > max) max = arr[i]; return max; } // Driver code public static void Main() { Console.WriteLine( "Largest in given " + "array is " + largest()); } } // This code is contributed by anuj_67. |
Javascript
<script> // JavaScript program to find // maximum in arr[] of size n function largest(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]; } return max; } // 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> |
PHP
<?php // PHP program to find maximum // in arr[] of size n // PHP function to find maximum // in arr[] of size n function largest( $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 ?> |
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.
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++
// C++ program to find maximum in arr[] of size n #include <bits/stdc++.h> using namespace std; // Function to find the largest element int largest( int arr[], int n, int i) { // Last index return the element if (i == n - 1) { return arr[i]; } // Find the maximum from rest of the array int recMax = largest(arr, n, i + 1); // Compare with i-th element and return return max(recMax, arr[i]); } // Driver Code int main() { int arr[] = { 10, 324, 45, 90, 9808 }; int n = sizeof (arr) / sizeof (arr[0]); // Function call cout << "Largest in given array is " << largest(arr, n, 0); return 0; } // This Code is contributed by Rajdeep Mallick |
Java
// Java program to find maximum in arr[] of size n import java.io.*; class GFG { // Function to find the largest element static int largest( int arr[], int n, int i) { // Last index return the element if (i == n - 1 ) { return arr[i]; } // Find the maximum from rest of the array int recMax = largest(arr, n, i + 1 ); // Compare with i-th element and return return Math.max(recMax, arr[i]); } // Driver Code public static void main(String[] args) { int arr[] = { 10 , 324 , 45 , 90 , 9808 }; int n = arr.length; // Function call System.out.println( "Largest in given array is " + largest(arr, n, 0 )); } } // this code is contributed by rajdeep999 |
Python3
# Python program to find maximum in arr[] of size n import math # Function to find the largest element def largest(arr, n, i): # Last index return the element if (i = = n - 1 ): return arr[i] # Find the maximum from rest of the array recMax = largest(arr, n, i + 1 ) # Compare with i-th element and return return max (recMax, arr[i]) # Driver Code if __name__ = = '__main__' : arr = [ 10 , 324 , 45 , 90 , 9808 ] n = len (arr) # Function call print ( "Largest in given array is " + str (largest(arr, n, 0 ))) # This code is contributed by aadityaburujwale. |
C#
// C# program to find maximum in arr[] of size n using System; public class GFG { // Function to find the largest element public static int largest( int [] arr, int n, int i) { // Last index return the element if (i == n - 1) { return arr[i]; } // Find the maximum from rest of the array var recMax = GFG.largest(arr, n, i + 1); // Compare with i-th element and return return Math.Max(recMax, arr[i]); } // Driver Code public static void Main(String[] args) { int [] arr = { 10, 324, 45, 90, 9808 }; var n = arr.Length; // Function call 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 function largest(arr, n, i) { // last index // return the element if (i == n - 1) { return arr[i]; } // find the maximum from rest of the array let recMax = largest(arr, n, i + 1); // compare with i-th element and return return Math.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__ |
PHP
<?php // PHP program to find maximum in arr[] of size n // Returns maximum in arr[] of size n function largest( $arr , $n , $i ) { // last index // return the element if ( $i == $n - 1) { return $arr [ $i ]; } // find the maximum from rest of the array $recMax = largest( $arr , $n , $i + 1); // compare with i-th element and return return max( $recMax , $arr [ $i ]); } // Driver Code $arr = [ 10, 324, 45, 90, 9808 ]; $n = count ( $arr ); echo "Largest in given array is " , largest( $arr , $n , 0) ; // This code is contributed by ameyabavkar02. ?> |
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
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++
// C++ program to find maximum in arr[] of size n #include <bits/stdc++.h> 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]); // Function call cout << largest(arr, n); return 0; } |
Java
// Java program to find maximum in arr[] of size n import java.io.*; import java.util.*; class GFG { // Returns maximum in arr[] of size n static int largest( int [] arr, int n) { Arrays.sort(arr); return arr[n - 1 ]; } // Driver code static public void main(String[] args) { int [] arr = { 10 , 324 , 45 , 90 , 9808 }; int n = arr.length; // Function call 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 def largest(arr, n): return max (arr) # Driver code if __name__ = = '__main__' : arr = [ 10 , 324 , 45 , 90 , 9808 ] n = len (arr) # Function call print (largest(arr, n)) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to find maximum in arr[] of size n using System; using System.Linq; public class GFG { // Returns maximum in arr[] static int Largest( int [] arr) { return arr.Max(); } // Driver code static public void Main() { int [] arr = { 10, 324, 45, 90, 9808 }; Console.WriteLine(Largest(arr)); } } // This code is contributed by anuj_67. // Minor code cleanup by shanmukha7 |
Javascript
<script> // Javascript program to find maximum in arr[] of size n // Returns maximum in arr[] of size n 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)); //This code is contributed by Mayank Tyagi </script> |
PHP
<?php // PHP program to find maximum in arr[] of size n // Returns maximum in arr[] of size n function largest( $arr , $n ) { return max( $arr ); } // Driver Code $arr = array (10, 324, 45, 90, 9808); $n = count ( $arr ); echo largest( $arr , $n ); // This code is contributed by anuj_67. ?> |
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.
Refer below article for more methods: Program to find the minimum (or maximum) element of an array
Contact Us