Largest element in an Array (original) (raw)
Last Updated : 27 Dec, 2024
Given an array **arr. 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
Table of Content
- Iterative Approach - O(n) Time and O(1) Space
- Recursive Approach - O(n) Time and O(n) Space
- Using Library Methods - O(n) Time and O(1) Space
Iterative Approach - O(n) Time and O(1) Space
The approach to solve this problem is to traverse the whole array and find the maximum among them.
C++ `
#include #include using namespace std;
int largest(vector& arr) { int max = arr[0];
//Traverse from second and compare
// every element with current max
for (int i = 1; i < arr.size(); i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main() { vector arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; }
C
#include <stdio.h>
int largest(int arr[], int n) { int i; 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;
}
int main() { int arr[] = { 10, 324, 45, 90, 9808 }; int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", largest(arr, n));
return 0;
}
Java
import java.io.*;
class GfG { static int largest(int[] arr) { int max = arr[0];
// Traverse array elements from second and
// compare every element with current max
for (int i = 1; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void main(String[] args) {
int arr[] = { 10, 324, 45, 90, 9808 };
System.out.println(largest(arr));
}
}
Python
def largest(arr, n): 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
if name == 'main': arr = [10, 324, 45, 90, 9808] n = len(arr)
ans = largest(arr, n)
print(ans)
C#
using System; using System.Collections.Generic;
class GfG { static int Largest(List arr) { int max = arr[0];
// Traverse from second and compare
// every element with current max
for (int i = 1; i < arr.Count; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
static void Main() {
List<int> arr = new List<int> { 10, 324, 45, 90, 9808 };
Console.WriteLine(Largest(arr));
}
}
JavaScript
function largest(arr) { let max = arr[0];
// Traverse from second and compare
// every element with current max
for (let i = 1; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr));
`
Recursive Approach - O(n) Time and O(n) Space
The idea is similar to the iterative approach. Here the traversal of the array is done recursively instead of an iterative loop.
C++ `
#include #include using namespace std;
int findMax(vector& arr, int i) {
// Last index returns the element if (i == arr.size() - 1) { return arr[i]; }
// Find the maximum from the rest of the vector
int recMax = findMax(arr, i + 1);
// Compare with i-th element and return
return max(recMax, arr[i]);
}
int largest(vector& arr) { return findMax(arr,0); }
int main() { vector arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; }
C
#include <stdio.h>
int findMax(int arr[], int size, int i) {
// Last index returns the element
if (i == size - 1) {
return arr[i];
}
// Find the maximum from the rest of the array
int recMax = findMax(arr, size, i + 1);
// Compare with i-th element and return
return (recMax > arr[i]) ? recMax : arr[i];
}
int largest(int arr[], int size) { return findMax(arr, size, 0); }
int main() { int arr[] = {10, 324, 45, 90, 9808}; int size = sizeof(arr) / sizeof(arr[0]); printf("%d", largest(arr, size)); return 0; }
Java
import java.util.Arrays;
class GfG { static int findMax(int[] arr, int i) {
// Last index returns the element
if (i == arr.length - 1) {
return arr[i];
}
// Find the maximum from the rest of the array
int recMax = findMax(arr, i + 1);
// Compare with i-th element and return
return Math.max(recMax, arr[i]);
}
static int largest(int[] arr) {
return findMax(arr, 0);
}
public static void main(String[] args) {
int[] arr = {10, 324, 45, 90, 9808};
System.out.println(largest(arr));
}
}
Python
def findMax(arr, i):
# Last index returns the element
if i == len(arr) - 1:
return arr[i]
# Find the maximum from the rest of the list
recMax = findMax(arr, i + 1)
# Compare with i-th element and return
return max(recMax, arr[i])
def largest(arr): return findMax(arr, 0)
if name == 'main': arr = [10, 324, 45, 90, 9808] print(largest(arr))
C#
using System;
class GfG { static int FindMax(int[] arr, int i) {
// Last index returns the element
if (i == arr.Length - 1) {
return arr[i];
}
// Find the maximum from the rest of the array
int recMax = FindMax(arr, i + 1);
// Compare with i-th element and return
return Math.Max(recMax, arr[i]);
}
static int Largest(int[] arr) {
return FindMax(arr, 0);
}
static void Main() {
int[] arr = {10, 324, 45, 90, 9808};
Console.WriteLine(Largest(arr));
}
}
JavaScript
function findMax(arr, i) { // Last index returns the element if (i === arr.length - 1) { return arr[i]; }
// Find the maximum from the rest of the array
let recMax = findMax(arr, i + 1);
// Compare with i-th element and return
return Math.max(recMax, arr[i]);
}
function largest(arr) { return findMax(arr, 0); }
// Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr));
`
Using Library Methods - O(n) Time and O(1) Space
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.
C++ `
#include #include #include using namespace std;
int largest(vector& arr) { return *max_element(arr.begin(), arr.end()); }
int main() { vector arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; }
C
#include <stdio.h> #include <stdlib.h>
int compare(const void* a, const void* b) { return ((int)a - (int)b); }
int largest(int arr[], int n) { qsort(arr, n, sizeof(int), compare); return arr[n - 1]; }
int main() { int arr[] = {10, 324, 45, 90, 9808}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", largest(arr, n)); return 0; }
Java
import java.io.; import java.util.;
class GfG {
static int largest(int[] arr) {
Arrays.sort(arr);
return arr[arr.length - 1];
}
static public void main(String[] args) {
int[] arr = { 10, 324, 45, 90, 9808 };
System.out.println(largest(arr));
}
}
Python
Python program to find maximum in arr[] of size n
def largest(arr): return max(arr)
if name == 'main': arr = [10, 324, 45, 90, 9808] print(largest(arr))
C#
using System; using System.Linq; using System.Collections.Generic;
class GfG { static int largest(List arr) { return arr.Max(); }
static public void Main() {
List<int> arr = new List<int> { 10, 324, 45, 90, 9808 };
Console.WriteLine(largest(arr));
}
}
JavaScript
// Function to find the largest number in an array function largest(arr) { return Math.max(...arr); }
// Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr));
`