Last Updated: 17 May, 2022

Largest Element in the Array

Easy
Asked in companies
MakeMyTripOracleMorgan Stanley

Problem statement

Given an array ‘arr’ of size ‘n’ find the largest element in the array.


Example:

Input: 'n' = 5, 'arr' = [1, 2, 3, 4, 5]

Output: 5

Explanation: From the array {1, 2, 3, 4, 5}, the largest element is 5.

Input Format :

The first line will contain a single integer 'n', the size of the array ‘arr’ 

The second line will contain ‘n’ integers representing the array elements.

Output format :

Print the maximum of elements.

Note :

You don't need to print anything. It has already been taken care of. Just implement the given function.

Approaches

01 Approach

We can create a temporary variable ‘temp’ and traverse the array and update the temporary variable ‘temp’ with the max of the elements.


 

The steps are as follows:-

  1. Create a temporary variable 'maxElement' and initialize it with 0.
  2. Traverse the array ‘arr’.
  3. Update ‘maxElement’ with the max of the current element of arr or 'maxElement'.
  4. Return 'maxElement'.