Beauty of programming-2.13-Maximum Product of child Arrays

Source: Internet
Author: User

1. Brief Introduction

Given an integer array whose length is N, only multiplication is allowed. Division is not enough to calculate the largest group of products in any (N-1) combination, and the time complexity of the algorithm is written.

2. Ideas

Division is not required in the question. In fact, it is a method that denies all the numbers and then removes each number separately. In fact, this method is not feasible because if there is 0 in the number, the sum of them is 0, and the Division is 0, which is troublesome. In addition, the overflow of values is also troublesome. In fact, as long as you find a combination of the number of N-1, do not need to calculate the product of this N-1 number.

The method is to first calculate the numbers of positive numbers, negative numbers, and 0, as well as the subscript of the maximum positive number, the subscript of the minimum positive number, the subscript of the maximum negative number, the subscript of the minimum negative number, and the subscript of any 0. Then we will discuss the situation as follows:
If the number of 0 is greater than 1, then the product of the number of N-1 must be 0, then randomly select a number of N-1, select Array [0]-Array [N-2] can be.
· If the number of 0 is equal to 1 and the number of negative numbers is odd, the product is either 0 or negative. Of course, 0 is greater, so remove the remaining number of N-1 for any negative number. If the number of negative numbers is an even number, then the product is either 0, or is a positive number, of course, a greater positive number, so remove the number of N-1 of this 0 can be.
· If the number of 0 values is less than 1 and the number of negative numbers is an odd number, the product is either a positive number or a negative number, removing a negative number (that is, the maximum negative number) with the smallest absolute value. If the number of negative numbers is an even number and there is no positive number at present, the result must be a negative number. Then, remove the negative number (that is, the smallest negative number) with the maximum logarithm value. If the number of negative numbers is an even number, if there is a positive number, remove a positive number with the smallest absolute value to ensure that the result is a positive number.

3. Code

This is ignored.

4. Reference

The beauty of programming, section 2.13, the maximum product of child Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.