Finding the Greatest Element in an Array
Problem Statement
Given an array of integers, find the greatest (maximum) element in the array.
Example
- Test Cases:
- Input:
[1, 2, 3, 4, 5]
| Output: 5
- Input:
[5, 4, 3, 2, 1]
| Output: 5
- Input:
[-3, -5, -7, -2, -8, -1, -4]
| Output: -1
- Input:
[100]
| Output: 100
- Input:
[]
| Output: undefined
(Empty Array)
Note :-
All methods discussed have a time complexity of O(n), but the space complexity varies. The brute force and built-in methods are more straightforward and efficient in terms of space, while the recursive method uses more space due to the recursive stack.