2023 HUAWEI OD Machine Test Exam B Volume [Maximum Difference of Split Array] Java Implementation

topic

Given an array nums consisting of several integers, which can be split at any position in the array, the array is split into two non-empty sub-arrays (ie, the left array and the right array), and the sub-arrays are summed to obtain two values , calculate the difference between these two values, and please output the value with the largest difference among all split schemes.
Input description
The number n of elements in the input array in the first line, 1 < n <= 100000

The second line inputs a sequence of numbers, separated by spaces, and the numbers are 4-byte integers.
Output description
The maximum value of the output difference
Example 1:
Input:

6
1 -2 3 4 -9 7

Output:
10

Explanation:
The feasible solutions for dividing the array nums into two non-empty arrays are: left array = [1] and right array = [-2,3,4,-9,7], and the difference = |1 - 3 |=2
left array = [1,-2] and right array = [3,4,-9,7], sum difference =| -1-5 |=6
left array = [1,-2,3 ,1] and right array=[4,-9,7], sum difference=|2 - 2|=0
left array=[1,-2,3,4] and right array=[-9,7 ], sum difference=|6 -(-2)| = 8,
left array=[1,-2,3,4,-9] and right array=[7], sum difference=|-3 -7| = 10 the maximum difference is 10

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/131203249