<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:
#         def getSum(value):
#             count = 0
#             # left side sum
#             if value > index:  # value is big enough to not reach 1 at the start of the array
#                 count += (value + (value - index)) * value // 2
#             else:  # value is not big enough -> need to have mulitple 1's
#                 count += (1 + value) * value // 2 + (index - value + 1)
#             # right side sum
#             if value > n - index - 1:
#                 count += (value + (n - index - value))









# idx = 4, value = 3, n = 8
# 1 1 1 2 3 2 1 1


















class Solution:
    def maxValue(self, n: int, index: int, maxSum: int) -> int:
        def getSum(value):  # value: current value of the number of index, decided by binary search
            count = 0
            # On index's left
            if value > index:
                # value: the value at index itself (is included as the rightmost element at the left side)
                count += (value + value - index) * (index + 1) // 2
            else:
                count += (value + 1) * value // 2 + (index - value + 1)
            # One index's right
            if value > n - index - 1:
                # value - (n - 1 - index): the value at the last index when decreasing by 1 each step from index
                count += (value + (value - (n - 1 - index))) * (n - index) // 2
            else:
                # n - index: total number of elements to the right of index
                # value: the number of elements where the decreasing sequence reaches 1
                count += (value + 1) * value // 2 + (n - index - value)
            return count - value  # value is added twice

        # Use binary search to find "value"
        left, right = 1, maxSum
        while left < right:
            mid = (left + right + 1) // 2  # +1: avoid infinite loop since "left is set to mid" below
            if getSum(mid) <= maxSum:
                left = mid
            else:
                right = mid - 1
        return left

24