Problem

Given an array arr of size N, find the largest (maximum) element in the array.

Examples

Example 1

1
2
3
Input: arr = [10, 20, 4]
Output: 20
Explanation: Among 10, 20, and 4, 20 is the largest.

Example 2

1
2
3
Input: arr = [-5, 0, 12, 8, -2]
Output: 12
Explanation: The largest value is 12.

Solution

Method 1 – Sorting

Intuition

Sorting the array puts the largest element at the end. The last element after sorting is the maximum.

Approach

  1. Sort the array in ascending order.
  2. Return the last element as the answer.

Code

1
2
3
4
5
6
7
class Solution {
public:
  int largestElement(vector<int>& arr) {
    sort(arr.begin(), arr.end());
    return arr.back();
  }
};
1
2
3
4
func largestElement(arr []int) int {
  sort.Ints(arr)
  return arr[len(arr)-1]
}
1
2
3
4
5
6
class Solution {
  public int largestElement(int[] arr) {
    Arrays.sort(arr);
    return arr[arr.length - 1];
  }
}
1
2
3
4
5
6
class Solution {
  fun largestElement(arr: IntArray): Int {
    arr.sort()
    return arr.last()
  }
}
1
2
3
4
class Solution:
  def largestElement(self, arr: list[int]) -> int:
    arr.sort()
    return arr[-1]
1
2
3
4
5
6
impl Solution {
  pub fn largest_element(arr: &mut [i32]) -> i32 {
    arr.sort();
    *arr.last().unwrap()
  }
}

Complexity

  • ⏰ Time complexity: O(n log n), due to sorting the array of size n.
  • 🧺 Space complexity: O(1) if sorting in-place, otherwise O(n) if a new array is created.

Method 2 – Linear Scan

Intuition

The largest element can be found by scanning the array and keeping track of the maximum seen so far.

Approach

  1. Initialize a variable ans with the first element of the array.
  2. Iterate through the array:
  • If the current element is greater than ans, update ans.
  1. Return ans after the loop.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
public:
  int largestElement(vector<int>& arr) {
    int ans = arr[0];
    for (int i = 1; i < arr.size(); ++i) {
      if (arr[i] > ans) ans = arr[i];
    }
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
func largestElement(arr []int) int {
  ans := arr[0]
  for _, v := range arr[1:] {
    if v > ans {
      ans = v
    }
  }
  return ans
}
1
2
3
4
5
6
7
8
9
class Solution {
  public int largestElement(int[] arr) {
    int ans = arr[0];
    for (int i = 1; i < arr.length; i++) {
      if (arr[i] > ans) ans = arr[i];
    }
    return ans;
  }
}
1
2
3
4
5
6
7
8
9
class Solution {
  fun largestElement(arr: IntArray): Int {
    var ans = arr[0]
    for (i in 1 until arr.size) {
      if (arr[i] > ans) ans = arr[i]
    }
    return ans
  }
}
1
2
3
4
5
6
7
class Solution:
  def largestElement(self, arr: list[int]) -> int:
    ans = arr[0]
    for x in arr[1:]:
      if x > ans:
        ans = x
    return ans
1
2
3
4
5
6
7
8
9
impl Solution {
  pub fn largest_element(arr: &[i32]) -> i32 {
    let mut ans = arr[0];
    for &x in &arr[1..] {
      if x > ans { ans = x; }
    }
    ans
  }
}

Complexity

  • ⏰ Time complexity: O(n), since each element is checked once.
  • 🧺 Space complexity: O(1), only a constant amount of extra space is used.