Mark As Completed Discussion

In this tutorial, we'll learn to find out the minimum and maximum values in an array using the following techniques.

  • Built-in Methods
  • Linear Search Method
  • Divide and Conquer
  • Single Loop Trick/ Comparison in Pairs

We'll be using a few different languages to solve the problem. So without further ado, let's get started!

Introduction

Built-in Methods

Rather than solving each and every problem manually, there are built-in methods available in programming languages which makes it easier for programmers to write code.

Let's have a look at the built-in methods available in Python, JavaScript, and Java to find the minimum and maximum value in an array.

  • Python has built-in functions min() and max() to find the minimum and maximum value in an array.
  • JavaScript has a built-in Math object that includes methods and constants for performing mathematical operations. We'll use Math.min() and Math.max() to find the minimum and maximum value in an array. Note that if we pass an array as an argument to these functions, it will return NaN. We can avoid this by using the spread operator to unwrap the elements.
  • In Java, we can use Collections.min() and Collections.max() methods. But since this method requires a list, so first, we'll convert an array to a list and then pass the list to these functions as an argument.

Now let's have a look at examples using the built-in methods available in these languages:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Are you sure you're getting this? Click the correct answer from the options.

What is the name of Python's built-in function to find the maximum value in an array?

Click the option that best answers the question.

  • findMax()
  • max()
  • findMaximum()
  • None of the above

Linear Search Method

In the Linear Search method, we'll implement the following steps.

  • Initialize the minimum and maximum values to the first element of the array.
  • Traverse the array except for the first element.
  • Update the minimum and maximum values after comparing them with the values in the array on each iteration accordingly.

Let's have a look at the Pseudocode first:

Pseudocode

SNIPPET
1int[] findMinimumAndMaximum(int input[], int size)
2{
3    int maximum = input[0]
4    int minimum = input[0]
5    for ( i = 1 to size-1 )
6    {
7        if ( input[i] > maximum )
8            maximum = input[i]
9        else if ( input[i] < minimum )
10            minimum = input[i]
11    }
12    int result[2] = {maximum, minimum}
13    return result
14}

Now, we'll implement the above steps in some other languages:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Are you sure you're getting this? Click the correct answer from the options.

In the Linear Search method, the maximum and minimum value is initialized to the last element of the array.

Click the option that best answers the question.

  • True
  • False

Divide and Conquer

In Divide and Conquer method, we'll implement the following steps.

  • Divide the array into two parts.
  • Find the minimum and maximum of both the parts recursively.
  • Take the minimum and maximum values of both the parts, compare them, and then find the minimum and maximum value of the entire array.

Next, we'll have a look at the Pseudocode:

Pseudocode

SNIPPET
1int[] findMinimumAndMaximum(int begin, int end,int input[])
2{
3    int maximum;
4    int minimum;
5    if ( begin == end )
6    {
7        minimum = input[begin]
8        maximum = input[begin]
9    }
10    else if ( begin + 1 == end )
11    {
12        if ( input[begin] < input[end] )
13        {
14            minimum = input[begin]
15            maximum = input[end]
16        }
17        else
18        {
19            minimum = input[end]
20            maximum = input[begin]
21        }
22    }
23    else
24    {
25        int midpoint = begin + (end - begin)/2
26        int leftArray[] = findMinimumAndMaximum(begin, midpoint, input)
27        int rightArray[] = findMinimumAndMaximum(midpoint+1, end, input)
28        if ( leftArray[0] > rightArray[0] )
29            maximum = leftArray[0]
30        else
31            maximum = rightArray[0]
32        if ( leftArray[1] < rightArray[1] )
33            minimum = leftArray[1]
34        else
35            minimum = rightArray[1]
36    }
37    
38    int result[2] = {maximum, minimum}
39   return result
40}

Now let's head straight to the implementation in Python, JavaScript, and Java:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Are you sure you're getting this? Click the correct answer from the options.

How many parts the array is divided into in Divide and Conquer method?

Click the option that best answers the question.

  • 3
  • 4
  • 1
  • None of the above

Single Loop Trick/Comparison in Pairs

In the Comparison in Pairs method, we'll implement the following steps.

  • If the size of the array is odd, then we'll initialize the minimum and maximum values to the first element of the array.
  • If the size is even, then we'll compare the first and second elements of the array and initialize minimum and maximum values accordingly.
  • Next, we'll traverse the array in pairs.
  • After that, we'll find the minimum and maximum value of each pair and then update the minimum and maximum value of the entire array accordingly.

Let's have a look at the Pseudocode:

Pseudocode

SNIPPET
1int[] findMinimumAndMaximum(int input[], int size)
2{
3    int maximum, minimum
4    int index
5    if ( size % 2 ==0)
6    {
7      if ( input[0] < input[1] )
8         {
9            maximum = input[1]
10            minimum = input[0]
11         }
12         else
13         {
14            maximum = input[0]
15            minimum = input[1]
16          }
17       index = 2   
18    }
19    else
20    {
21        maximum = input[0]
22        minimum = input[0]
23        index = 1      
24    }
25    while ( index < size )
26    {
27        if ( input[index] < input[index+1] )
28        {
29            if ( input[index] < minimum )
30                minimum = input[index]
31            if ( input[index+1] > maximum )
32                maximum = input[index+1]
33        }
34        else
35        {
36            if ( input[index] > maximum )
37                maximum = input[index]
38            if ( input[index+1] < minimum )
39                minimum = input[index+1] 
40        }
41        index = index + 2
42    }
43    
44    int result[2] = {maximum, minimum}
45   return result
46}

Next, we'll implement the above steps in Python, Javacript, and Java:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Let's test your knowledge. Click the correct answer from the options.

If the array size is odd in Comparison in Pairs method, the maximum and minimum value is initialized to which element of the array?

Click the option that best answers the question.

  • First
  • Second
  • Last
  • None of the above

Conclusion

With this, we have come to the end of our tutorial. Here, we learned to find out the minimum and maximum values in an array using the Built-in, Linear Search, Divide and Conquer, and Comparison in Pairs methods. We also implemented each method in Python, JavaScript, and Java.

One Pager Cheat Sheet

  • We will learn to find the maximum and minimum values in an array using built-in methods, linear search, divide and conquer and a single-loop trick in various languages.
  • Using the built-in methods of Python, JavaScript, and Java, we can easily determine the minimum and maximum value in an array.
  • Python has two built-in functions, min() and max(), to find the minimum and maximum value in an array, respectively.
  • The Linear Search Method traverses an array from the second element to find the maximum and minimum values, which can be implemented using the Pseudocode provided and other languages to obtain the desired result.
  • The Linear Search algorithm's maximum and minimum values must be initialized to the first element of the array, as it traverses through the array from the beginning to find the maximum and minimum values.
  • The Divide and Conquer method can be used to find the minimum and maximum value of an array by recursively splitting it into two parts and comparing their results.
  • The Divide and Conquer method divides the array into two parts of varying sizes, eventually resulting in the determination of the overall minimum and maximum values.
  • We can find the minimum and maximum values of an array using the Comparison in Pairs method which involves initializing the minimum and maximum values based on the size of the array and then traversing the array in pairs within a single loop.
  • The maximum and minimum value of the array is initialized to input[0], the first element, when the array size is odd.
  • We concluded the tutorial by learning the Built-in, Linear Search, Divide and Conquer, and Comparison in Pairs methods for finding minimum and maximum values in an array and implementing these methods in Python, JavaScript and Java.