Do you need to know how to find the greatest product of three numbers of the array? Take a look at our Python array example and you will know how to cope with your assignment. Assignment.EssayShark offers you the opportunity to use free samples from our blog to complete your own assignments. All the examples are completed by experienced tutors who are knowledgeable in certain spheres. Thus, the explanation on how to find the greatest product of three numbers of the array that you’ll see below was completed by an IT expert.

If after getting acquainted with our Python array example you still have difficulties with the issue, you can apply directly to us. Our service can provide you with unique samples completed within your specific requirements. The service is absolutely safe – you don’t need to worry about your privacy. Contact us as soon as you need help! Assignment.EssayShark is available 24/7!

**The Maximum Product of Three Numbers of the Array**

We have an array of integers including negative numbers. We need to find the greatest product of three numbers of the array.

For example, we have the array int_arr, containing numbers -10, -10, 1, 3, 2. The function that handles the array should return 300 as -10 * -10 * 3 = 300. We need to perform the task as efficiently as possible not forgetting the negative numbers.

Solution

There are many methods to solve this problem, but it is not so easy to achieve O(n) running time and O(1) storage costs. To solve the problem effectively we will create and monitor the status of the following variables:

- high_product_3
- high_product_2
- high
- low_product_2
- low

When we go through the array to the end, high_product_3 will contain the answer, and other variables will be used as temporary buffers. High_product_2 and low_product_2 will contain the greatest product of the two and the smallest product of the two numbers respectively, and passing through the array, we will check the product of the current number and these variables (negative current with low_product_2 and the positive current with high_product_2). We need the high and the low to remember the minimum and maximum numbers in the array.

Here is the solution implemented in Python:

def high_product_three(int_arr): # check the amount of the elements if len(int_arr) < 3: raise Exception('There are less than 3 elements in the list') # start from the third element # 2 first elements will be assigned to # high_product_2 and low_product_2. high = max(int_arr[0], int_arr[1]) low = min(int_arr[0], int_arr[1]) high_product_2 = int_arr[0] * int_arr[1] low_product_2 = int_arr[0] * int_arr[1] # calculate the high_product_3 using the first three elements high_product_3 = int_arr[0] * int_arr[1] * int_arr[2] # pass through the array from the 2nd element for current in int_arr[2:]: # check the ability to enlarge high_product_3 high_product_3 = max( high_product_3, current * high_product_2, current * low_product_2) # check the same about high_product_2 high_product_2 = max( high_product_2, current * high, current * low) # and low_product_2 low_product_2 = min( low_product_2, current * high, current * low) # is there a new maximum? high = max(high, current) # is there a new minimum? low = min(low, current) return high_product_3

The complexity of the algorithm is O(n) execution time and O(1) memory.

Thanks for your attention!