<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:
        """
        n = 6, index = 1, maxSum = 10
        3, 4, 3, 2, 1, 0 ==> sum = 13 > maxSum (X)
        2, 3, 2, 1, 0, 0 ==> sum = 8 < maxSum (O)

        sum of consectutive numbers
        total = (a0 + an) * n / 2
        """

        def getSum(value, index, n):
            print("getSum: value, index, n", value, index, n)
            # get minimum sum on both sides
            left_dis = index
            right_dis = n - index - 1
            # value on index 0
            val_0 = max(value - left_dis, 1)
            # value on index n-1
            val_n = max(value - right_dis, 1)
            print("left_dis, right_dis", left_dis, right_dis)
            if val_0 == 1:
                # start adding from index of 1
                sum_left = (1 + value) * value  / 2
                # add ones
                sum_left += (index - value + 1)
            else:
                sum_left = (val_0 + value) * (left_dis+1) / 2

            if val_n == 1:
                sum_right = (1 + value) * value / 2
                # add ones
                sum_right += (right_dis - value + 1)
            else:
                sum_right = (val_n + value) * (right_dis+1) / 2

            print("val_0, val_n, sum_left, sum_right", val_0, val_n, sum_left, sum_right)
            return sum_left + sum_right - value          

        val1, val2 = 0, maxSum
        while val1 < val2:
            mid = (val1 + val2+1)//2
            sum1 = getSum(mid, index, n)
            if sum1 <=  maxSum:
                val1 = mid 
            else:
                val2 = mid - 1
        
        return val1
64