Hello, everyone! Today, we will discuss the problem type that often appears in coding tests, which is ‘Making Maximum Value by Grouping Numbers’. This topic greatly helps in developing algorithmic thinking and enhancing problem-solving skills.
Problem Description
This problem involves selecting all numbers from a given integer array to make the maximum value. You can group numbers and multiply them, and if you group two numbers A and B, the new number will be A * B. How should we group the numbers to create the maximum value? What is the optimal method?
Problem Examples
Input: [1, 2, 3, 4, 5] Output: 120 (1*2*3*4*5 = 120) Input: [0, 0, 0, 1, 2] Output: 2 (1*2 = 2, the remaining 0s make the product 0) Input: [1, 2, 3, -1, -2] Output: 6 (1*2*3 or -1*-2*3 possible)
Problem Analysis
The key to this problem is how to multiply the selected numbers. Therefore, the solutions include:
- Group as many positive numbers as possible, and group negative numbers depending on the situation.
- If 0 is included, avoid situations where multiplication results in 0.
- Be cautious with 1, as it does not increase the product when multiplied by other numbers.
Algorithm Approach
The main steps to construct the algorithm are as follows:
- Separate positive numbers, negative numbers, and 1 from the input array.
- Multiply positive numbers as much as possible.
- Negative numbers can also be paired and multiplied.
- 1 should be either added to the product of positive numbers or managed separately with the product of negative numbers.
- Finally, return the calculated result.
Implementation
Now let’s implement this in Python:
def max_product(nums): positive = [] negative = [] zero_count = 0 product = 1 has_negative = False for num in nums: if num > 1: positive.append(num) elif num == 1: continue elif num < 0: negative.append(num) has_negative = True else: zero_count += 1 # Multiply positives for p in positive: product *= p # Pair and multiply negatives negative.sort() if len(negative) % 2 == 1: negative.pop() # Exclude one if odd for n in negative: product *= n return product if product != 1 or (zero_count == 0 and not has_negative) else 0 # Test print(max_product([1, 2, 3, 4, 5])) # 120 print(max_product([0, 0, 0, 1, 2])) # 2 print(max_product([1, 2, 3, -1, -2])) # 6
Code Explanation
The code above iterates through the array, categorizing each number into positive, negative, or zero. Then, it multiplies all positive numbers and only pairs of negative numbers to accumulate the result. Ultimately, if the resulting value is 1, it returns either 0 or 1 for special cases (arrays consisting only of 0s or 1s).
Complexity Analysis
The time complexity of this algorithm is O(N), as it checks each number only once. The space complexity is also O(N), as it stores positive and negative numbers in separate arrays.
Conclusion
Today, we learned how to develop algorithmic thinking through the ‘Making Maximum Value by Grouping Numbers’ problem. It is essential to guide the derivation of optimal results by considering various inputs, which can further enhance your coding skills. In the next session, we will cover another interesting problem. Thank you!