Maximum And Minimum Elements Of An Array

Nilesh Saini
2 min readMay 22, 2022

--

Different methods to find the maximum and then minimum element of an array

Photo by Hamish Duncan on Unsplash

Built-in methods:

Max and Min Pointers

  1. This function will accept an array as an argument.
  2. Initialize the max and the min variable.
  3. Iterate through the array and if the current element is greater than the current max then, set the max to the current element.
  4. And if the current element is less than the current min then, set the min to the current element.
  5. 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.

--

--

Nilesh Saini
Nilesh Saini

Written by Nilesh Saini

Web Developer/ Front-end engineer who loves solving Rubik's Cube

No responses yet