Maximum And Minimum Elements Of An Array
Different methods to find the maximum and then minimum element of an array
Built-in methods:
Max and Min Pointers
- This function will accept an array as an argument.
- Initialize the max and the min variable.
- Iterate through the array and if the current element is greater than the current max then, set the max to the current element.
- And if the current element is less than the current min then, set the min to the current element.
- Return the max and min variables.
Optimized Max and Min Pointers
1. Create and initialize max and min variables.
2. Create another set of maximum and minimum variables.
3. Create a variable to store the last element. (If the length of the array is odd)
4. Loop through the array, two elements at a time.
5. If the first element is greater than the second element then set maximum and minimum accordingly.
6. Compare maximum and minimum elements to max and min and change if required.
7. If the length of the array is odd then compare the max and min to the last element and make changes accordingly.
7. return max and min.