Swift Coding Test Course, Making Maximum Value by Grouping Numbers

Problem Definition

Write a function to create the maximum value by grouping numbers from the given array of numbers. In this problem, the way to group numbers is to either select each number, select two numbers to add, or select three or more numbers to multiply. You need to write the code considering the ways to create the maximum value.

Input and Output Format

Input

An integer N (1 ≤ N ≤ 1000) and an array containing N integers are provided.

Output

Print the maximum value.

Example

Input

    5
    1 2 3 4 5
    

Output

    120
    

Approach to the Problem

To solve this problem, it is important to consider that multiplying two or more numbers has a significant impact on increasing the maximum value when combining numbers. Accordingly, the array of numbers should be sorted, and the maximum value should be tracked to find the optimal combination. The basic approach is as follows:

  1. Sort the array in ascending order.
  2. Calculate the maximum value by adding or multiplying from the end of the array.
  3. In particular, when there are consecutive numbers greater than or equal to zero, it is advantageous to multiply.
  4. Handle cases that include 0 or 1 separately to calculate the maximum value accurately.

Implementation Steps

Now, based on the above approach, let’s implement the Swift code. Below is the code to solve the problem.

    func maximumValue(from numbers: [Int]) -> Int {
        let sortedNumbers = numbers.sorted()
        var maxValue = 0
        var i = sortedNumbers.count - 1
        
        while i >= 0 {
            if i > 0, sortedNumbers[i] > 1, sortedNumbers[i - 1] > 1 {
                maxValue += sortedNumbers[i] * sortedNumbers[i - 1]
                i -= 2
            } else {
                maxValue += sortedNumbers[i]
                i -= 1
            }
        }
        
        return maxValue
    }
    
    // Example Input
    let inputNumbers = [1, 2, 3, 4, 5]
    let result = maximumValue(from: inputNumbers)
    print(result) // Output: 120
    

Code Explanation

The code above defines the function `maximumValue` that creates the maximum value from the given array of numbers. The function performs the following tasks:

  1. Sorts the array in ascending order.
  2. Starts from the end of the array to calculate the maximum value by multiplying two at a time or adding one at a time.
  3. Finally returns the calculated maximum value.

Test Cases

Let’s check the accuracy of the code through various test cases.

    let testCases = [
        [1, 2, 3, 4, 5],
        [0, 2, 5, 1, 8],
        [1, 1, 1],
        [2, 2, 2, 3],
        [-1, -2, -3, -4],
        [0, 1, 2, 3, 4]
    ]
    
    for testCase in testCases {
        print("Input: \(testCase) => Maximum Value: \(maximumValue(from: testCase))")
    }
    

Conclusion

In this lecture, we covered the problem of ‘Creating Maximum Value by Grouping Numbers’. Through the process of solving the problem, we understood the importance of how to combine numbers and were able to derive optimal results. By solving similar problems often found in coding tests, you can enhance your skills. I hope you continue to solve various algorithm problems and build a deeper understanding.