<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </strong>that satisfies the following conditions:</p>

<ul>
	<li><code>nums.length == n</code></li>
	<li><code>nums[i]</code> is a <strong>positive</strong> integer where <code>0 &lt;= i &lt; n</code>.</li>
	<li><code>abs(nums[i] - nums[i+1]) &lt;= 1</code> where <code>0 &lt;= i &lt; n-1</code>.</li>
	<li>The sum of all the elements of <code>nums</code> does not exceed <code>maxSum</code>.</li>
	<li><code>nums[index]</code> is <strong>maximized</strong>.</li>
</ul>

<p>Return <code>nums[index]</code><em> of the constructed array</em>.</p>

<p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x &gt;= 0</code>, and <code>-x</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, index = 2,  maxSum = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> nums = [1,2,<u><strong>2</strong></u>,1] is one array that satisfies all the conditions.
There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 6, index = 1,  maxSum = 10
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= maxSum &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= index &lt; n</code></li>
</ul>

class Solution:
    def maxValue(self, n: int, index: int, maxSum: int) -> int:
        # 在index的地方遍历1-maxSum，二分找结果
        # set nums[index] to target, nums[index-i] to target-i, and nums[index+i] to target-i
        # 计算和，然后二分答案
        def calculate_sum(mid: int, length: int) -> int:
            if mid > length:
                # The decreasing sequence reaches 1 before the length
                total = (mid + mid - length + 1) * length // 2
            else:
                # The decreasing sequence reaches 1 and the rest are filled with 1s
                total = (mid + 1) * mid // 2 + (length - mid)
            return total

        left_len = index           # Number of elements to the left of the index
        right_len = n - index - 1  # Number of elements to the right of the index

        left, right = 1, maxSum
        answer = 1  # Minimum possible value at nums[index]

        while left <= right:
            mid = (left + right) // 2 
            total = mid  # Start with the value at nums[index]

            # Calculate the sum of the left side
            total += calculate_sum(mid - 1, left_len)
            # Calculate the sum of the right side
            total += calculate_sum(mid - 1, right_len)

            if total <= maxSum:
                answer = mid       # Update the answer with the current mid
                left = mid + 1     # Try to find a larger value
            else:
                right = mid - 1  # Try to find a smaller value

        return right
        
34