Statement▼
Given three positive integers, n, index, and maxSum, output the nums[index] by constructing an array of nums with the length of n, which satisfies the following conditions:
The length of the array
numsis equal ton.Each element
nums[i]is a positive integer, where1 ≤ i< n.The absolute difference between two consecutive elements,
nums[i]andnums[i+1], is at most1 .The sum of all elements in
numsdoes not exceedmaxSum.The element at
nums[index]contains the maximum value.
Constraints:
1 ≤ n≤ maxSum≤ 109 0 ≤ index< n
Solution
The solution to maximizing the value at a given index in a bounded array involves using a binary search approach combined with mathematical calculations of arithmetic sequences. To achieve this, we need to ensure that the values on both sides of nums[index] decrease by at most
The algorithm to solve this problem is as follows:
Initialization
Initialize two variables,
leftandright, with the values1 andmaxSum, respectively. We can assign a minimum value of1 to an element, and the maximum value ofmaxSum.Start a loop and Iterate until
leftis less thanright, and calculate the sum of left and right elements as listed below:
Calculate mid
Calculate
midasmid=(left+right+1)/2 .
Determine values on the left side
If
midis greater thanindex, there will be a sequence starting frommidto themid−index (right to left). This sequence will form a series[mid−index,mid−index+1,mid−1,...,mid] , which is an arithmetic series. This arithmetic series is calculated as(mid+mid−index)∗(index+1)/2.
For example, if n = 5, index = 4, and maxSum = 20, then mid will equal
Otherwise, if
midis less than or equal toindex, then there will be a sequence starting frommidto1 (right to left). This sequence will form a series[1,2,3,...,mid−1,mid] , which is an arithmetic series. In mathematics, this arithmetic series is calculated as(mid+1)∗mid/2 . In addition to the arithmetic series, there will also be a sequence of1 s of lengthindex−mid+1 .
For example, if n = 8, index = 7, maxSum = 9, then mid will equal
Moreover, there will be
The total sum of the left side is calculated by
Determine values on the right side
If
midis greater than or equal ton - index, there will be a sequence starting frommidtomid−n+1+index (left to right). This sequence will form a series[mid,mid−1,mid−2,...,mid−n+1+index] , which is an arithmetic series. In mathematics, this arithmetic series is calculated as(mid+mid−n+1+index)∗(n−index)/2 .
For example, if n = 10, index = 7, and maxSum = 11, then mid will equal
Otherwise, if
midis less thann - index, there will be a sequence starting frommidto1. This sequence will form a series[mid,mid−1,...,2,1] , which is an arithmetic series. In mathematics, this arithmetic series is calculated as(mid+1)∗mid/2 . In addition to the arithmetic series, there will also be a sequence of1 s of lengthn−index−mid .
For example, if n = 8, index = 2, maxSum = 9, then mid will equal
Moreover, there will be
The total sum of the right side is calculated by
Sum calculation
Calculate the total sum of the array by combining the sum of both sides and subtracting the value of
mid. The reason for subtractingmidis that it is calculated twice (in the sum of the left and the sum of the right).
Updating variables
If the sum of the array is less than or equal to the
maxSum, we will use the right half of the array by updating theleftwith the value ofmid.Otherwise, we will go with the left half of the array by updating
rightwith the value ofmid - 1.
After the loop terminates, we will return the value of left because it is the index of the maximum value of nums.
Let’s look at the following illustration to get a better understanding of the solution: