Texts: Given an array arr of n positive integers, the following operation can be performed any number of times. Use a 1-based index for the array.
Choose any i such that 2 <= i <= n.
Choose any x such that 1 <= x <= a / sqrt(i).
Set arr[i-1] to arr[i-1] + x.
Set arr[i] to arr[i] - x.
Minimize the maximum value of arr using the operation and return the value.
Example:
n = 4
arr = [1, 5, 7, 6]
Assuming 1-based indexing.
One optimal sequence is:
Operation 1: choose i = 3, x = 4 (note that x <= arr[3], i.e. 4 < 7)
- Replace arr[i-1] with arr[i-1] + x or 5 + 4 = 9
- Replace arr[i] with arr[i] - x or 7 - 4 = 3
The array is now [1, 9, 3, 6] (maximum = 9)
Operation 2: i = 2, x = 4
- Replace arr[i-1] with 1 + 4 = 5
- Replace arr[i] with 9 - 4 = 5
The array is now [5, 5, 3, 6] (maximum = 6)
Operation 3: i = 4, x = 1
The resulting array is [5, 5, 4, 5] (maximum = 5)
The minimum possible value of max(arr) is 5 after operation 3.
Function Description:
Complete the function getMaximum in the editor below.
getMaximum has the following parameter:
- arr[n]: an array of integers
Returns:
- t: the minimum maximum value possible.