An array A is given, the task is to find minimum and maximum element of A.
Example 1: Find max and min element of: {1 ,2 ,3 ,4 ,5}
Input: {1 ,2 ,3 ,4 ,5}
Output: [1,5]
Example 1: Find max and min element of: {1}
Input: {1}
Output: [1,1]
Example 1: Find max and min element of: {52 ,94 ,3 ,18 ,5}
Input: {52 ,94 ,3 ,18 ,5}
Output: [3,94]
Solutions
Method 1: Linear Search
We can solve this problem in O(n) time using simple linear search, all we need to do is to compare and update each element with min & max variables. Initialise min and max with 0th element and iterate the array from index 1 to length-1.
Complexity
The time complexity of this solution is O(n) and space complexity is O(1).