Problem of the day
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.
6
4 7 8 6 7 6
8
The answer is 8.
From {4 7 8 6 7 6}, 8 is the largest element.
8
5 9 3 4 8 4 3 10
10
O(n), Where ‘n’ is the size of an input array ‘arr’.
1 <= 'n' <= 10^5
1 <= 'arr[i]' <= 10^9
Time Limit: 1 sec
Think about how to iterate through the array and keep track of the largest element encountered so far.
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:-
O(n), Where ‘n’ is the size of an input array ‘arr’.
We are traversing the array of size ‘n’
.
Hence time complexity is O(n).
O(1).
We are creating a temporary variable that takes constant space with respect to array size.
Hence space complexity is O(1).